簡體   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