php - 在 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
解决方案
我认为与其选择在请求对象上getPrefix
使用该方法的方式,is()
其中 is 方法执行以下操作
is 方法允许您验证传入请求 URI 是否与给定模式匹配。使用此方法时,您可以使用 * 字符作为通配符:
if (request()->is('cp/*')) {
// do the template switching.
}
推荐阅读
- mercurial - hg 标签的返回值是多少 --template "{date|isodate}\n"
- android - 即使在观察者被移除后,实时数据观察者也会被调用
- python - 对python中的部分数据框做一些处理
- firebase - 如何在 Firebase Functions shell 中运行 Pub-Sub 函数
- python - 如何在python中正确导入包?
- python - 如何在熊猫中旋转这个数据框
- python - 无法在 Python 中发出 HTTP 请求
- android - IndexOutOfBoundsException: Invalid index 0, size is 0. 我该如何解决这个问题
- c# - 如何解决 WACK 问题“发现错误:私有代码签名测试遇到以下错误:”
- javascript - Vuex状态在突变后不更新