CakePHP 3 Custom Route in Pagination

穿精又带淫゛_ 提交于 2021-02-08 11:02:55

问题


in my routes file i define a route for my controller method

$routes->connect('/category/:cat', ['controller' => 'Categories', 'action' => 'category']);

My controller method is this

public function category(){
        $this->paginate = [
                            'limit' => 2
                            ];
        $this->viewBuilder()->layout('newLayout');
        $cat = $this->request->params['cat'];
        $id = $this->Categories->findBySlug($cat)->select(['id'])->hydrate(false)->toArray();
        $cid = $id[0]['id'];

        $category = $this->ProductCategories
                    ->find("all")
                    ->select(['id', 'category_id'])
                    ->where(["category_id" => $cid])
                    ->contain(['Products' => function($q){
                                return $q->select(['id', 'sku', 'product', 'description', 'slug', 'price', 'off', 'stock', 'product_category_id'])
                                         ->where(['status' => 1])
                                         ->contain(['ProductImages' => function($q){
                                                return $q->select(['product_id','url']);
                                            }]);
                            }])->hydrate(false);

        $categories = $this->paginate($category);

        $this->set(compact('categories'));
        $this->set('_serialize', ['categories']);
    }

And my url look like this:

http://localhost/mizzoli.com/category/Designer-Saree

Now when i click on cake pagination url change to this

http://localhost/mizzoli.com/categories/category?page=2

But actual url i want is like this

http://localhost/mizzoli.com/category/Designer-Saree?page=2

And also i need to pass some extra parameter with pagination url like color, occasion etc. Please help me to get this. I did not find any solution.


回答1:


I had a similar issue and resolved it by passing the param I needed inside the third parameter of the route definition. Like this:

$routes->connect('/category/:cat', ['controller' => 'Categories', 'action' => 'category'], ['pass' => ['cat']]);

I have @littleylv from the #cakephp IRC channel on Freenode to thank for this solution.

You can read more about passing parameters to actions via routes here: https://book.cakephp.org/3.0/en/development/routing.html#passing-parameters-to-action



来源:https://stackoverflow.com/questions/39871520/cakephp-3-custom-route-in-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!