1

我在尝试让我的蛋糕应用程序根据页面的 url 在单个页面上显示数据库中的项目时遇到问题...例如,我想显示“派对”类别中的项目,然后输入“mydomain” .com/All/party”,然后我会在该页面上获得该类别中所有项目的列表。

这是我的代码,但我从路线获得的页面是空白的:

我的路线.php:

    Router::connect('/categories/:name', array(
   'controller' => 'All', 'action' => 'categories'
),
array(
    'pass' => 'catname',
    'catname' => '[a-zA-Z0-9]+'
));

我的 AllController.php:

    function categories($catname = null) {
$options = array(
    'conditions' => array('Category.name'=>$catname)
);
$Category = $this->Category->find('all', $options);
$this->set('Category', $Category);
$this ->render('/All/categories');
}

任何帮助,将不胜感激。

4

3 回答 3

2

我完全没有得到您刚刚发布的所有代码,但是您想要实现的目标非常简单:

Router::connect(
    '/products/:category', 
    ['controller' => 'products', 'action' => 'categorized'], 
    ['pass' => ['category']]
);

在 ProductsController 中:

function categorized($category) {
    $conditions = ['Category.name' => $category];
    $this->set('products', $this->Prodcut->find('all', compact('conditions')));
}

而已。如果您不需要单独的视图模板,您可能希望重用 index 操作,在这种情况下,只需设置条件(例如 Paginator 组件)并调用$this->setAction('index')

于 2013-12-03T15:30:07.110 回答
0

我找到了一种更简单的方法来做我想做的事。我只记得我有一个数据库搜索选项,允许人们搜索产品和类别。我所做的只是简单地搜索类别,然后复制我获得的页面的 URL,并将其设置为我的应用程序中的类别的链接。

示例代码:

<ul class="filter-options">
  <li><a style="color:#fff" href="http://mydomain.com/products/search?q=parties">Parties</a></li>
  <li><a style="color:#fff" href="http://mydomain.com/products/search?q=festivals">Festivals</a></li>
  <li><a style="color:#fff" href="http://mydomain.com/products/search?q=theatres">Cinemas/Theatres</a></li>

于 2013-12-05T17:04:48.177 回答
0

一切看起来都不错,只需更改您的 routes.php 如下

Router::connect('/categories/:catname', array(
        'controller' => 'all', 
        'action' => 'categories'),
            array('pass' => array('catname'),
                'catname' => '[a-zA-Z0-9]+'));
于 2016-06-28T08:14:44.120 回答