0

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.

7
  • Have you tried $request->route('parameter_name') or $request->route()->parameters()? Commented Feb 12, 2017 at 18:42
  • @BenSwinburne I get Undefined variable: request Commented Feb 12, 2017 at 19:14
  • You'd need to get the request object request() should give you it. request()->route() Commented Feb 12, 2017 at 19:30
  • @BenSwinburne $request is now defined but the parameters return null. i.imgur.com/J9vB5TW.png Commented Feb 12, 2017 at 19:41
  • @BenSwinburne My mistake, it is still not defined. Code placement was wrong. Commented Feb 12, 2017 at 19:43

2 Answers 2

1

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' %}
Sign up to request clarification or add additional context in comments.

4 Comments

octobercms.com/docs/database/model#query-scopes Here under Dynamic Scopes it shows how to applyType, but it doesn't work. I put it in my Page htm php, and directed it at my custom Component. $user = Gallery::applyType('john')->get(); It says "Call to undefined method October\Rain\Database\QueryBuilder::applyType()".
I will try your solution and add your code to my component.
@MattMcManis how did you write your scope method in your model? It needs to be defined in the form of public function scopeApplyType($query, $type)
You've got me very close to solving this, here is an extension to the question showing the scope. stackoverflow.com/q/42337725/6806643
0

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

9 Comments

I used OctoberCMS to create the Page. When I use Route::get() in routes.php, it overrides my Page with a blank one. I used the dd() with 'username' and it returned a blank page with the word 'null'. But 'slug' returned 'user/john', the whole uri.
I can get the Identifier {username?} with $username = $this->param('username') only when used in the Page php, but how to use that same code in the Model or send to the Scope?
What does $this refer to? the above way is the Laravel 5 way to do it so it should work. What version of Laravel are you using?
If I echo $this in the Page php, it returns "Object of class Cms58a1e3092320e853718389_1418827154Class could not be converted to string". It must be some sort of Page Class October is creating?
Presumably, yes. I'm not familiar with OctoberCMS but i'd have assumed that the standard Laravel stuff would work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.