[英]How to parse URL segment Identifiers in Laravel?
I'm using OctoberCMS based on Laravel. 我正在使用基于Laravel的OctoberCMS。
I'm trying to capture segments of the URL to pass into a Scope Function to filter Database Results. 我正在尝试捕获URL的各个段,以传递给作用域函数以过滤数据库结果。
Visiting localhost/user/john/nature would parse and filter back those results. 访问localhost / user / john / nature将解析并过滤掉这些结果。
URL → Identifiers → Param Variables → Scope → Database Results URL→标识符→参数变量→作用域→数据库结果
Page 页
[builderDetails builderUser]
identifierValue "{{ :username }}"
[builderDetails builderCategory]
identifierValue "{{ :category }}"
[builderList]
scope = "scopeApplyType"
url = /user/:username?/:category?/:page?
Model Scope 型号范围
I want to filter Database Results using URL Identifers :username and :category. 我想使用URL标识符:username和:category过滤数据库结果。
public function scopeApplyType($query) {
$params = ['username' => $username, 'category' => $category];
return $query->where($params);
}
Get Identifiers 获取标识符
This will output the requested identifers in the URL, in routes.php 这将在route.php中的URL中输出请求的标识符。
Route::get('user/{username?}/{category?}', function ($username = null, $category = null) {
echo $username;
echo $category;
});
Output 输出量
localhost/user/john/nature
john
nature
Soultion? 灵魂?
A Route::get() won't work, I need something inside or passed to the Scope to define the params variables. Route :: get()无法正常工作,我需要在内部或传递给Scope的东西来定义params变量。
Something like: 就像是:
$username = '{username?}';
$username = request()->url('username');
$username = $this->param('username'); //Components)
$username = $this->route('username');
$username = \Route::current()->getParameter('username');
All return null or error. 全部返回null或错误。
Like the way you would normally parse a Query 就像您通常解析查询的方式一样
$param = "username=john&category=nature";
$username = $category = '';
parse_str($param);
echo $username;
echo $category;
Or similar to Request Segment 或类似于请求细分
$username = request()->segment(2); //user/:username
segment(2) is a static location unlike {:category} which can change position on different URL's. segment(2)是一个静态位置,与{:category}不同,它可以更改不同URL上的位置。
What you are trying to do has gone beyond the scope of the very basic components provided by the builder plugin . 您试图做的事超出了builder插件提供的非常基本的组件的范围。
You should now look at creating your own Components within your plugin. 现在,您应该考虑在插件中创建自己的组件。 See http://octobercms.com/docs/plugin/components and http://octobercms.com/docs/cms/components for further information, as well as the section on routing parameters specifically
有关更多信息,请参见http://octobercms.com/docs/plugin/components和http://octobercms.com/docs/cms/components ,以及有关路由参数的部分
A very basic example of your custom component might look like this: 您的自定义组件的一个非常基本的示例可能如下所示:
<?php namespace MyVendor/MyPlugin/Components;
use Cms\Classes\ComponentBase;
use MyVendor\MyPlugin\Models\Result as ResultModel;
class MyComponent extends ComponentBase
{
public $results;
public function defineProperties()
{
return [
'categorySlug' => [
'title' => 'Category Slug',
'type' => 'string',
'default' => '{{ :category }}',
],
'username' => [
'title' => 'Username',
'type' => 'string',
'default' => '{{ :username }}',
],
];
}
public function init()
{
$this->results = $this->page['results'] = $this->loadResults();
}
public function loadResults()
{
return ResultModel::where('username', $this->property('username'))
->where('category', $this->property('categorySlug'))
->get();
}
}
Then in your component's default.htm view you'd do something like this: 然后,在组件的default.htm视图中,您将执行以下操作:
{% for result in __SELF__.results %}
{{ result.title }}
{% endfor %}
And in your OctoberCMS page: 在您的OctoberCMS页面中:
url = /user/:username?/:category?/:page?
[myComponent]
categorySlug = "{{ :category }}"
username = "{{ :username }}"
==
{% component 'myComponent' %}
Given the following route 给出以下路线
Route::get('foo/{name?}', function ($name = null) {
dd(request()->route()->parameters());
});
When visiting /foo/bar the output is as below. 当访问/ foo / bar时,输出如下。
array:1 [▼
"name" => "bar"
]
Similarly you can use 同样,您可以使用
dd(request()->route()->parameter('name')); // bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.