首页 文章

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

提问于
浏览
0

我正在使用基于Laravel的OctoberCMS .

我正在尝试捕获URL的片段以传递到Scope函数以过滤数据库结果 .

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

URL→标识符→参数变量→范围→数据库结果

Routing Parameters

Query Scopes

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 回答

  • 1

    您要做的事情超出了builder plugin提供的基本组件的范围 .

    您现在应该考虑在插件中创建自己的组件 . 有关详细信息,请参阅http://octobercms.com/docs/plugin/componentshttp://octobercms.com/docs/cms/components,以及section on routing parameters specifically

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

    <?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' %}
    
  • 0

    鉴于以下路线

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

    访问/ foo / bar时输出如下 .

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

    同样你可以使用

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

相关问题