从另一个目录加载资产

时间:2017-05-05 08:47:31

标签: php laravel .htaccess

我目前正试图找出以下困境。

示例:

访问网址:http://localhost/admin/content/1/builder
iframe网址(在页面上):http://localhost/templates/silver-1/index.html

现在在index.html内部会请求资产,如:

<link rel="stylesheet" href="css/site.css">

我当然无法使用{{asset(&#39; css / site.css&#39;)}}因为这是一个.html文件,它将用于以后获取&的页面构建器#34;编译&#34;到blade.php文件。

当我&#34;编译&#34;而不是替换所有资产网址。如果以这种方式请求我想要重定向的页面:

http://localhost/1/css/site.css&gt; http://localhost/templates/silver-1/css/site.css http://localhost/1/some_sub_dir/css/site.css&gt; http://localhost/templates/silver-1/css/site.css

现在我使用以下路线进行了 令人讨厌的 解决方法:

Route::get('/{id}/{dir?}/{path?}', function($id, $dir = null, $path = null) {
    $page = Session::get('Site.current.page');
    $location = Session::get('Site.current.template_location');

    if(!$page || $page != $id) {
        if(is_numeric($id)) {
            $page = Frontend\Models\Page::find($id);
            $location = $page->template->template_location;
            Session::put('Site.current.page', $page->id);
            Session::put('Site.current.template_location', $location);
        }
    }

    $p = plugin_path('Frontend', sprintf('templates/%s/%s/%s', $location, $dir, $path));

    if(!file_exists($p)) {
        return null;
    }

    return Redirect::to(plugin_asset('Frontend', sprintf('templates/%s/%s/%s', $location, $dir, $path)));
})
->where('id', '[0-9]')
->where('dir', 'js|css|img|font|vendors|stylesheets|images|assets|js-files')
->where('path', '(.*)');

现在请原谅我,因为我自己发现代码可怕,破碎和垃圾,因此我为什么要寻找更好的方法来解决它。使用.htaccess或其他...

我尝试在laravel .htaccess中组合重定向语句,如下所示:

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]

无论我使用.htaccess做什么,我都要打破它(500 / laravel routing borked)

非常感谢任何帮助,如果需要更多代码或说明,请在下面发表评论。

提前谢谢。

0 个答案:

没有答案