Cakephp Routing for SEO URLs in version 3.7

I was using custom route controller like above in cakephp2.x, but now I have upgraded the cakephp. I would like to know how to acheive the same in cakephp3.7

earlier I used

> class CategoryRoute extends CakeRoute
> {
>     /**
>      * Settings for this object.
>      *
>      * - `fields` The fields to use to identify a route by.
>      * - `model` The model name of the Route, defaults to Route.
>      * - `scope` Additional conditions to use when looking up a route,
>      *    e.g. `array('Route.active' => 1).`
>      *
>      * @var array
>      */
>     public $settings = array(
>         'fields' => array(
>            
>             'value' => 'value',
>             'slug'=>'slug',
>             'id'=>'id'
>         ),
>         'model' => 'Category',
>         'scope' => array()
>     );
>     /**
>      * Constructor
>      *
>      * @return void
>      */
>     public function __construct($settings = array())
>     {
>         $this->settings = Set::merge($this->settings, $settings);
>     }
>     /**
>      * Match
>      *
>      * @param array $url An array of parameters to check matching with
>      * @return mixed Either a string url for the parameters if they match or false
>      */
>     public function match($url)
>     {
>         if (empty($url)) {
>             return false;
>         }
> 
>        $params = array(
>             $url['plugin'],
>             $url['controller'],
>             $url['action']
>         );
>         unset($url['plugin']);
>         unset($url['controller']);
>         unset($url['action']);
>         ksort($url, SORT_NUMERIC);
>      
>         foreach ($url as $val) {
>             $params[] = $val;
>         }
>         $url_pp=implode('/', array_filter($params));
>         
>         
> 
>         $conditions = array(
>             $this->settings['model'] . '.' . $this->settings['fields']['value'] => implode('/', array_filter($params)),
>         );
> 
>         if (!empty($this->settings['scope'])) {
>             $conditions = array_merge($conditions, $this->settings['scope']);
>         }
>         App::import('Model', "Product");
>         $result = ClassRegistry::init($this->settings['model'])->field($this->settings['fields']['slug'], $conditions);
>  
>        
> 
>         if (empty($result)) {
>             return false;
>         }else{
>           
>             $category=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$params['3'])));
>       
> if(!empty($category['Category']['slug'])){
>             $slug_cat[]=$category['Category']['slug'];
>      if(!empty($category['Category']['parent_id'])){ 
>        $child1=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$category['Category']['parent_id'])));
>         $slug_cat[]=$child1['Category']['slug'];
>        if(!empty($child1['Category']['parent_id'])){ 
>          $child2=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$child1['Category']['parent_id'])));
>           $slug_cat[]=$child2['Category']['slug']; 
>           if(!empty($child2['Category']['parent_id'])){ 
>              $child3=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$child2['Category']['parent_id'])));
>                      $slug_cat[]=$child3['Category']['slug'];
>              if(!empty($child3['Category']['parent_id'])){
>                   $child4=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$child3['Category']['parent_id'])));
>                   $slug_cat[]=$child4['Category']['slug'];
>                 
>              }
>           }
>         
>        }
>      }
>    $cattt=  array_reverse($slug_cat, true);
>    if(!is_numeric($cattt)){
>     $comma_separated = implode('/', $cattt); 
>      // debug($comma_separated);  
>        // debug($cattt);
>     $res=$comma_separated;
> 
>      }else{
>         $res="";
>      }
>       //$comma_separated = implode('/', $cattt); 
> 
>    //debug($comma_separated);
> //$res=$comma_separated;
> 
> 
> 
>   }else{
>     return false;
>   }
>         }
> 
>    
>         return $res;
>     }
>     /**
>      * Parse
>      *
>      * @param string $url The url to attempt to parse
>      * @return mixed Boolean false on failure, otherwise an array of parameters
>      */
>     public function parse($url)
>     {
>     $urls = explode('/', $url);
>         if(!isset($urls['1'])){
>             return false;
>          }
> 
>          if($urls['1']=="users"&&isset($urls['1'])){
>             return false;
>          }
> 
>         if($urls['1']=="orders"&&isset($urls['1'])){
>             return false;
>          }
>          
>          
>          
>         $conditions = array(
>             $this->settings['model'] . '.' . $this->settings['fields']['slug'] => end($urls),
>         );
>         if (!empty($this->settings['scope'])) {
>             $conditions = array_merge($conditions, $this->settings['scope']);
>         }
>         App::import('Model', $this->settings['model']);
>         $result = ClassRegistry::init($this->settings['model'])->field($this->settings['fields']['slug'], $conditions);
>         if (empty($result)) {
>             return false;
>         }else{
>         $category=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.slug'=>end($urls))));
> 
>             $slug_cat[]=$category['Category']['slug'];
>      if(!empty($category['Category']['parent_id'])){
>        $child1=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$category['Category']['parent_id'])));
>         $slug_cat[]=$child1['Category']['slug'];
>        if(!empty($child1['Category']['parent_id'])){
>          $child2=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$child1['Category']['parent_id'])));
>           $slug_cat[]=$child2['Category']['slug'];
>           if(!empty($child2['Category']['parent_id'])){
>              $child3=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$child2['Category']['parent_id'])));
>                      $slug_cat[]=$child3['Category']['slug'];
>              if(!empty($child3['Category']['parent_id'])){
>                   $child4=ClassRegistry::init($this->settings['model'])->find('first',array(
>     'conditions'=>array('Category.id'=>$child3['Category']['parent_id'])));
>                   $slug_cat[]=$child4['Category']['slug'];
>                 
>              }
>           }
>         
>        }
>      }
> 
>    $cattt=  array_reverse($slug_cat, true);
>  if(!is_numeric($cattt)){
>     $comma_separated = implode('/', $cattt); 
>      // debug($comma_separated);  
>        // debug($cattt);
>     $result="Products/liste/".$comma_separated;
> 
>      }else{
>         $result="Products/liste/".$cattt[0];
>      }
> //$result="Products/view/".$comma_separated."/".$product['Product']['slug']."-".$product['Brand']['slug']."/".$nbr.".html";    
>         }
>        
>         $parts = explode('/', $result);
>       
>         $count = count($parts);
>         if ($count >= 2) {
>             $params['controller'] = $parts[0];
>             $params['action'] = $parts[1];
>             $params['plugin'] = null;
>             $params['pass'] = $params['named'] = array();
>            /* for ($i = 2; $i < $count; $i++) {
>                 $params['pass'][] = $parts[$i];
>             }*/
>             $params['pass'][]=intval(substr($url, -10, 5));
>             return $params;
>         }
>         return false;
>     }
> }

And in routes used

App::import('Lib', 'CategoryRoute');
Router::connect('/paf/:page',array('controller' => 'Products','action' => 'liste'),array('pass' => array('paf'),'page' => '[0-9]+','routeClass' => 'CategoryRoute'));

to get the url like domain.com/category/subcategory/subsubcategory

Let me know how to get this in latest cakephp version

1 Like

Iā€™m having the same problem, did you manage to solve this?