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'
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程