首页 > 解决方案 > 在 app/providers 中访问路由前缀

问题描述

我有一个多模板网站,我在我的视图文件夹中为不同的模板创建了多个文件夹......但我有 1 个管理模板,我想从该文件夹中为所有人加载管理部分

在我的路线上,我有类似的东西

Route::group(['namespace' => 'Cp', 'prefix' => 'cp'  , 'middleware'=>['admin' ,'auth'] ], function()
{

    Route::get('/' ,  'IndexController@index' )->name('index_cp');
});

app/providers/ViewServiceProvider.php我根据数据库存储值动态更改模板文件夹中,但我试图获取当前路由的前缀,所以如果cp它忽略模板文件夹并从文件夹加载cp模板

    public function registerViewFinder()
    {
        $request = app(\Illuminate\Http\Request::class);
        dump($request->route()->getPrefix());
        if($request->route()->getPrefix() != 'cp')
        {
         // read from db &  set the template folder 
        }

    }

我收到这个错误

Call to a member function getPrefix() on null

基本上$request->route() 返回 null

标签: phplaravellaravel-5.4

解决方案


我认为与其选择在请求对象上getPrefix使用该方法的方式,is()

其中 is 方法执行以下操作

is 方法允许您验证传入请求 URI 是否与给定模式匹配。使用此方法时,您可以使用 * 字符作为通配符:

if (request()->is('cp/*')) {
   // do the template switching.
}

文档链接


推荐阅读