doutang3077 2013-04-19 15:19
浏览 26
已采纳

CakePHP:我可以用Router :: Promot或其他方式推广活动路由器吗?

I've got two or more routes that will be going to the same controller and action. This is fine until I want to use a helper such as the form helper or pagination on the page. What happens is that the current url changes to whatever is declared first in my routes.php file.

I see there is a way to promote a router with Router::promote but I'm not sure if I can do it based on the current url or router being used or if there's a bett way to do this.

Here's an example of what my router.php looks like:

Router::connect('/cars-for-sale/results/*', array('controller' => 'listings', 'action' => 'results'));
Router::connect('/new-cars/results/*', array('controller' => 'listings', 'action' => 'results'));
Router::connect('/used-cars/results/*', array('controller' => 'listings', 'action' => 'results'));

Let's say for example that I'm at the url domain.com/used-cars/results/ and I'm using the form helper or pagination helper, the url that is being put in the action or href is domain.com/cars-for-sale/results/.

Any help or info would be appreciated.

  • 写回答

1条回答 默认 最新

  • dongzhan5943 2013-04-19 22:51
    关注

    Routes should be unique and identifiable!

    The problem with these Routes is that, basically, you created duplicate URLs not only does this cause problems with CakePHP picking the right route, Google doesn't like that as well; duplicated content will have a negative effect on your SEO ranking!

    In order to pick the right URL (Route), CakePHP should be able to do so, based on its parameters; your current Routes do not offer any way to distinguish them.

    And neither does your application!

    All these URLs will present the same data;

    /cars-for-sale/results/
    /new-cars/results/
    /used-cars/results/
    

    Solution 1 - separate actions

    If your application is limited to these three categories, the easiest solution is to create three actions, one per category;

    Controller:

    class ListingsController extends AppController
    {
        const CATEGORY_NEW       = 1;
        const CATEGORY_USED      = 2;
        const CATEGORY_FOR_SALE  = 3;
    
        public $uses = array('Car');
    
    
        public function forSaleCars()
        {
            $this->set('cars', $this->Paginator->paginate('Car', array('Car.category_id' => self::CATEGORY_FOR_SALE)));
        }
    
        public function newCars()
        {
            $this->set('cars', $this->Paginator->paginate('Car', array('Car.category_id' => self::CATEGORY_NEW)));
        }
    
        public function usedCars()
        {
            $this->set('cars', $this->Paginator->paginate('Car', array('Car.category_id' => self::CATEGORY_USED)));
        }
    }
    

    Routes.php

    Router::connect(
        '/cars-for-sale/results/*', 
        array('controller' => 'listings', 'action' => 'forSaleCars')
    );
    Router::connect(
        '/new-cars/results/*', 
        array('controller' => 'listings', 'action' => 'newCars')
    );
    Router::connect(
        '/used-cars/results/*', 
        array('controller' => 'listings', 'action' => 'usedCars')
    );
    

    Solution 2 - Pass the 'category' as parameter

    If the list of URLs to be used for the 'listings' will not be fixed and will expand, it may be better to pass the 'filter' as a parameter and include that in your routes;

    routes.php

    Router::connect(
        '/:category/results/*',
        array(
             'controller' => 'listings',
             'action'     => 'results',
        ),
        array(
    
             // category: lowercase alphanumeric and dashes, but NO leading/trailing dash
             'category'  => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
    
             // Mark category as 'persistent' so that the Html/PaginatorHelper
             // will automatically use the current category to generate links
             'persist'   => array('category'),
    
             // pass the category as parameter for the 'results' action
             'pass'      => array('category'),
        )
    );
    

    Read about the Router API

    In your controller:

    class ListingsController extends AppController
    {
        public $uses = array('Car');
    
    
        /**
         * Shows results for the specified category
         *
         * @param string $category
         *
         * @throws NotFoundException
         */
        public function results($category = null)
        {
            $categoryId = $this->Car->Category->field('id', array('name' => $category));
    
            if (!$categoryId) {
                throw new NotFoundException(__('Unknown category'));
            }
    
            $this->set('cars', $this->Paginator->paginate('Car', array('Car.category_id' => $categoryId)));
        }
    }
    

    And, to create a link to a certain category;

    $this->Html->link('New Cars', 
        array(
             'controller' => 'listings', 
             'action'     => 'results', 
             'category'   => 'new-cars'
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。