我正在使用基于Laravel的OctoberCMS .
我正在尝试捕获URL的片段以传递到Scope函数以过滤数据库结果 .
访问localhost / user / john / nature会解析并过滤掉这些结果 .
URL→标识符→参数变量→范围→数据库结果
Page
[builderDetails builderUser]
identifierValue "{{ :username }}"
[builderDetails builderCategory]
identifierValue "{{ :category }}"
[builderList]
scope = "scopeApplyType"
url = /user/:username?/:category?/:page?
Model Scope
我想使用URL Identifers过滤数据库结果:username和:category .
public function scopeApplyType($query) {
$params = ['username' => $username, 'category' => $category];
return $query->where($params);
}
Get Identifiers
这将在routes.php中的URL中输出所请求的标识符
Route::get('user/{username?}/{category?}', function ($username = null, $category = null) {
echo $username;
echo $category;
});
产量
localhost/user/john/nature
john
nature
Soultion?
Route :: get()不起作用,我需要内部的东西或传递给Scope以定义params变量 .
就像是:
$username = '{username?}';
$username = request()->url('username');
$username = $this->param('username'); //Components)
$username = $this->route('username');
$username = \Route::current()->getParameter('username');
全部返回null或错误 .
就像你通常解析查询一样
$param = "username=john&category=nature";
$username = $category = '';
parse_str($param);
echo $username;
echo $category;
或类似于请求段
$username = request()->segment(2); //user/:username
segment(2)是一个静态位置,不像{:category},它可以改变不同URL的位置 .
2 回答
您要做的事情超出了builder plugin提供的基本组件的范围 .
您现在应该考虑在插件中创建自己的组件 . 有关详细信息,请参阅http://octobercms.com/docs/plugin/components和http://octobercms.com/docs/cms/components,以及section on routing parameters specifically
自定义组件的一个非常基本的示例可能如下所示:
然后在组件的default.htm视图中,你会做这样的事情:
在您的OctoberCMS页面中:
鉴于以下路线
访问/ foo / bar时输出如下 .
同样你可以使用