XF 2.2 Help with routing and pagination

grantus

Active member
I have my routing set up like this:

Code:
Route prefix: archives
Sub-name: cat
Route format: cat/:str<cat>/:page

And in my template for pagination I put for the link:
("$cat_params" is the "cat" in the route format)

Code:
link="archives/cat/{$cat_params}

But when I look at the pagination results it shows this:
("test" is my category):

Code:
archives/cat/page-2/test

Why isn't it showing the pagination after my $cat_params?
 
Solution
Do you have a separate category table?

Idiomatically, $category would be named $battlePoints since it's a collection of battle points entities. I had thought $category referred to some category entity which was a bit confusing.

I'm wondering why the pagination isn't working with the cat added to the URL automatically since it's being passed on through the $params->cat.
Nothing is handled automatically. Assuming you don't have a separate category table/entity, you would want to pass $params->cat to your template (via the view params) and then use the manual method in my previous post:

PHP:
$viewParams = [
    // ...
    'cat' => $params->cat,
];

HTML:
<xf:pagenav page="{$page}"...
Where $category is a category entity (presumably with a cat column), and $params is an array of URL query params:

HTML:
<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$total}"
    link="archives/cat" data="{$category}" params="{$params}"
    wrapperclass="block-outer-main" />
 
Where $category is a category entity (presumably with a cat column), and $params is an array of URL query params:

HTML:
<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$total}"
    link="archives/cat" data="{$category}" params="{$params}"
    wrapperclass="block-outer-main" />
I have $category as an entity. For $params do you mean the $viewParams?

This is what I have:

Code:
$viewParams = [
            'category' => $category->fetch(),
            'page' => $page,
            'perPage' => $perPage,
            'total' => $category->total(),
        ];
 
No, $params would just be your ?query=1&params=2 if you have any. If not you can omit it entirely.

FWIW $category will actually be a collection of entities. Use fetchOne instead of fetch to get back just a single entity.
 
It doesn't seem to be working. I have:

Code:
$category = $this->finder('Testing\ILL:BattlePoints')
->where('category', $params->cat)

<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$total}" 
link="archives/cat" data="{$category}" params="{$params}" wrapperclass="block-outer-main" />
 
It looks like your $category object is just a finder, which is really just a query builder. To get the entity, call fetchOne on the finder.

It looks fine otherwise, so if it still doesn't work you'll need to specify what result you are/aren't getting.
 
It looks like your $category object is just a finder, which is really just a query builder. To get the entity, call fetchOne on the finder.

It looks fine otherwise, so if it still doesn't work you'll need to specify what result you are/aren't getting.
fetchOne would only give one result so I need fetch instead since it's a list I'm querying.

I tried creating a variable $getcat = $params->cat and used that here data="{$getcat}" but I don't get anything. Even if I try putting data="test" directly into the template, it doesn't show.

So my pagination is still archives/cat/page-2 instead of archives/cat/test/page-2
 
You're displaying a list of categories then? What does test refer to, and where does it come from?

I tried creating a variable $getcat = $params->cat and used that here data="{$getcat}" but I don't get anything. Even if I try putting data="test" directly into the template, it doesn't show.
The data attribute expects an associative array of route parameters to values (normally it is an entity with columns matching the route parameters). Your route parameter is cat, so if you were setting it manually for whatever reason it would be:
HTML:
<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$total}"
    link="archives/cat" data="{{ {'cat': 'test'} }}"
    wrapperclass="block-outer-main" />
(The curly brace syntax is the associative array syntax for templates.)

Without posting more code or information I don't really have a clear picture of what you're trying to do and where it's going wrong.
 
You're displaying a list of categories then? What does test refer to, and where does it come from?


The data attribute expects an associative array of route parameters to values (normally it is an entity with columns matching the route parameters). Your route parameter is cat, so if you were setting it manually for whatever reason it would be:
HTML:
<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$total}"
    link="archives/cat" data="{{ {'cat': 'test'} }}"
    wrapperclass="block-outer-main" />
(The curly brace syntax is the associative array syntax for templates.)

Without posting more code or information I don't really have a clear picture of what you're trying to do and where it's going wrong.
I have my custom table that has a column named category.

I'm displaying a list of names based on whatever category is cat from my route.

Everything works fine except the pagination which is not showing the cat so it only shows page-2 instead of cat/test/page-2.

This is everything I have:

Code:
public function actionIndex (ParameterBag $params)
    {       
        $category = $this->finder('Testing\ILL:BattlePoints')
        ->where('category', $params->cat)
        ->order('battle_date', 'DESC');

        $page = $params->page;
        $perPage = 20;

        $category->limitByPage($page, $perPage);

        $viewParams = [
            'category' => $category->fetch(),
            'page' => $page,
            'perPage' => $perPage,
            'total' => $category->total(),
        ];

        return $this->view('Testing\ILL:ArchivesCat', 'archives_category', $viewParams);
    }

My routing is:

Code:
Route prefix: battles
Subname: archives/cat
Route format: archives/cat/:str<cat>/:page

I'm wondering why the pagination isn't working with the cat added to the URL automatically since it's being passed on through the $params->cat.
 
Do you have a separate category table?

Idiomatically, $category would be named $battlePoints since it's a collection of battle points entities. I had thought $category referred to some category entity which was a bit confusing.

I'm wondering why the pagination isn't working with the cat added to the URL automatically since it's being passed on through the $params->cat.
Nothing is handled automatically. Assuming you don't have a separate category table/entity, you would want to pass $params->cat to your template (via the view params) and then use the manual method in my previous post:

PHP:
$viewParams = [
    // ...
    'cat' => $params->cat,
];

HTML:
<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$total}"
    link="archives/cat" data="{{ {'cat': $cat} }}"
    wrapperclass="block-outer-main" />
 
Solution
Do you have a separate category table?

Idiomatically, $category would be named $battlePoints since it's a collection of battle points entities. I had thought $category referred to some category entity which was a bit confusing.


Nothing is handled automatically. Assuming you don't have a separate category table/entity, you would want to pass $params->cat to your template (via the view params) and then use the manual method in my previous post:

PHP:
$viewParams = [
    // ...
    'cat' => $params->cat,
];

HTML:
<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$total}"
    link="archives/cat" data="{{ {'cat': $cat} }}"
    wrapperclass="block-outer-main" />
It's just the battlepoints table and there's a category column in there.

I got it to work now with the data="{{ {'cat': $cat} }}" you provided. I had already passed the $params->cat but I just didn't know the proper syntax for data inside the url.

Thanks for all the help and sorry for the $category confusion.
 
Glad you got it working 🙂

I just didn't know the proper syntax for data inside the url.
Yes, it uses the regular link builder under the hood, which expects an associative array of route params to values. The syntax is the template equivalent of ['cat' => $cat] in PHP.

In the core, route params are usually pulled from entity properties (ie, if you did have a separate category entity with a cat column). Entities can behave like associative arrays, so typically the entity object itself is passed in. This doesn't seem applicable to your case, but just for reference if you stumble across it elsewhere.
 
Glad you got it working 🙂


Yes, it uses the regular link builder under the hood, which expects an associative array of route params to values. The syntax is the template equivalent of ['cat' => $cat] in PHP.

In the core, route params are usually pulled from entity properties (ie, if you did have a separate category entity with a cat column). Entities can behave like associative arrays, so typically the entity object itself is passed in. This doesn't seem applicable to your case, but just for reference if you stumble across it elsewhere.
Just out of curiosity, how would I pass two different params into data? In case that's ever needed someday.
 
Back
Top Bottom