FEATURE: add template helper for uploaded files url

This commit is contained in:
Michal Kváček
2016-04-04 20:41:46 +02:00
parent aff8e992d6
commit 090aed23cb
+33 -2
View File
@@ -1,6 +1,7 @@
<?php
namespace App\Helpers;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Process\Exception\InvalidArgumentException;
class Template
@@ -40,7 +41,37 @@ class Template
return $view;
}
public static function asset($path) {
return env('SITE_URL').'/assets/'.config('app.theme').'/'.$path;
/**
* Get URL to uploaded file
*
* @param $filename
*
* @return string
*/
public static function upload($filename)
{
// for debug environment check if file exists
if (env('APP_DEBUG') && !file_exists(config('app.upload_path') . '/' . $filename)) {
throw new NotFoundHttpException('File ' . $filename . ' cannot be found in ' . config('app.upload_path') . '.');
}
// return url to uploaded file
return config('app.upload_url') . '/' . $filename;
}
public static function asset($path)
{
if (env('APP_DEBUG')) {
if (empty($path)) {
throw new InvalidArgumentException('No asset name defined!');
}
// check if asset exists and is file
if (!file_exists(public_path('assets/' . config('app.theme') . '/' . $path))) {
throw new NotFoundHttpException('Cannot find any asset in ' . public_path('assets/' . config('app.theme') . '/' . $path) . '. Try to check your paths.');
}
}
return config('app.url') . '/assets/' . config('app.theme') . '/' . $path;
}
}