繁体   English   中英

如何在Laravel中解析URL段标识符?

[英]How to parse URL segment Identifiers in Laravel?

我正在使用基于Laravel的OctoberCMS。

我正在尝试捕获URL的各个段,以传递给作用域函数以过滤数据库结果。

访问localhost / user / john / nature将解析并过滤掉这些结果。

URL→标识符→参数变量→作用域→数据库结果

路由参数

查询范围

[builderDetails builderUser]
identifierValue "{{ :username }}"

[builderDetails builderCategory]
identifierValue "{{ :category }}"

[builderList]
scope = "scopeApplyType"

url = /user/:username?/:category?/:page?

型号范围

我想使用URL标识符:username和:category过滤数据库结果。

public function scopeApplyType($query) {
    $params = ['username' => $username, 'category' => $category];
    return $query->where($params);
}

获取标识符

这将在route.php中的URL中输出请求的标识符。

Route::get('user/{username?}/{category?}', function ($username = null, $category = null) {
    echo $username;
    echo $category;
});

输出量

localhost/user/john/nature

john
nature

灵魂?

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上的位置。

您试图做的事超出了builder插件提供的非常基本的组件的范围。

现在,您应该考虑在插件中创建自己的组件。 有关更多信息,请参见http://octobercms.com/docs/plugin/componentshttp://octobercms.com/docs/cms/components ,以及有关路由参数部分

您的自定义组件的一个非常基本的示例可能如下所示:

<?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();
    }
}

然后,在组件的default.htm视图中,您将执行以下操作:

{% for result in __SELF__.results %}
    {{ result.title }}
{% endfor %}

在您的OctoberCMS页面中:

url = /user/:username?/:category?/:page?

[myComponent]
categorySlug = "{{ :category }}"
username = "{{ :username }}"
==
{% component 'myComponent' %}

给出以下路线

Route::get('foo/{name?}', function ($name = null) {
    dd(request()->route()->parameters());
});

当访问/ foo / bar时,输出如下。

array:1 [▼
  "name" => "bar"
]

同样,您可以使用

dd(request()->route()->parameter('name')); // bar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM