CakePHP 3分页中的自定义路线

时间:2016-10-05 10:28:41

标签: php cakephp pagination cakephp-3.0

在我的路线文件中,我为我的控制器方法定义路线

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

我的控制器方法就是这个

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']);
    }

我的网址看起来像这样:

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

现在,当我点击蛋糕分页URL更改为此

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

但我想要的实际网址是这样的

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

而且我还需要传递一些额外的参数和分页网址,如颜色,场合等。请帮我解决这个问题。我没有找到任何解决方案。

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题并通过在路由定义的第三个参数中传递我需要的参数来解决它。像这样:

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

我从Freenode的#cakephp IRC频道获得@littleylv感谢这个解决方案。

您可以在此处阅读有关通过路线将参数传递给操作的更多信息:https://book.cakephp.org/3.0/en/development/routing.html#passing-parameters-to-action