简体   繁体   中英

How to parse URL segment Identifiers in Laravel?

I'm using OctoberCMS based on Laravel.

I'm trying to capture segments of the URL to pass into a Scope Function to filter Database Results.

Visiting localhost/user/john/nature would parse and filter back those results.

URL → Identifiers → Param Variables → Scope → Database Results

Routing Parameters

Query Scopes

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.

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::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.

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.

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.

What you are trying to do has gone beyond the scope of the very basic components provided by the builder plugin .

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

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:

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

And in your OctoberCMS page:

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.

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

Similarly you can use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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