dty98339 2016-10-26 11:19
浏览 92
已采纳

处理除Laravel资产之外的所有请求的路由

We are using Laravel 5 and have a route that handles all requests:

Route::any('{all}', 'AllController')
        ->where('all', '.*');

However this is causing one challenge: it redirects everything including images, JS and CSS.

We would like assets (static content) to be handled by the web server and Laravel to only handle dynamic content. We tried this:

Route::any('{all}', 'AllController')
        ->where('all', '.*')->where('all', !=, 'assets/.*');

But it gives syntax error unexpected '!=' (T_IS_NOT_EQUAL).

How can we construct a route that handles everything except assets (static content)?

Edit: .htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
  • 写回答

1条回答 默认 最新

  • doulao2916 2016-10-26 11:33
    关注

    It looks like you have trouble with web server config.. Laravel will handle route only if requested file doesn't exist. In other case web server will return your .css .js without Laravel. For Apache this rules looks like

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    UPD: In any case you can using regexp for this

    Route::any('{all}', 'MyBar@index')->where('all','^((?!assets).)*?');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料