Router::connectNamed PHP Method

connectNamed() public static method

Do not parse any named parameters: Router::connectNamed(false); Parse only default parameters used for CakePHP's pagination: Router::connectNamed(false, array('default' => true)); Parse only the page parameter if its value is a number: Router::connectNamed(array('page' => '[\d]+'), array('default' => false, 'greedy' => false)); Parse only the page parameter no matter what. Router::connectNamed(array('page'), array('default' => false, 'greedy' => false)); Parse only the page parameter if the current action is 'index'. Router::connectNamed( array('page' => array('action' => 'index')), array('default' => false, 'greedy' => false) ); Parse only the page parameter if the current action is 'index' and the controller is 'pages'. Router::connectNamed( array('page' => array('action' => 'index', 'controller' => 'pages')), array('default' => false, 'greedy' => false) ); ### Options - greedy Setting this to true will make Router parse all named params. Setting it to false will parse only the connected named params. - default Set this to true to merge in the default set of named parameters. - reset Set to true to clear existing rules and start fresh. - separator Change the string used to separate the key & value in a named parameter. Defaults to :
public static connectNamed ( array $named, array $options = [] ) : array
$named array A list of named parameters. Key value pairs are accepted where values are either regex strings to match, or arrays as seen above.
$options array Allows to control all settings: separator, greedy, reset, default
return array
    public static function connectNamed($named, $options = array())
    {
        if (isset($options['separator'])) {
            static::$_namedConfig['separator'] = $options['separator'];
            unset($options['separator']);
        }
        if ($named === true || $named === false) {
            $options += array('default' => $named, 'reset' => true, 'greedy' => $named);
            $named = array();
        } else {
            $options += array('default' => false, 'reset' => false, 'greedy' => true);
        }
        if ($options['reset'] || static::$_namedConfig['rules'] === false) {
            static::$_namedConfig['rules'] = array();
        }
        if ($options['default']) {
            $named = array_merge($named, static::$_namedConfig['default']);
        }
        foreach ($named as $key => $val) {
            if (is_numeric($key)) {
                static::$_namedConfig['rules'][$val] = true;
            } else {
                static::$_namedConfig['rules'][$key] = $val;
            }
        }
        static::$_namedConfig['greedyNamed'] = $options['greedy'];
        return static::$_namedConfig;
    }

Usage Example

Example #1
0
 /**
  * undocumented function
  *
  * @return void
  */
 function index()
 {
     $this->set('disableNav', true);
     Router::connectNamed(array('type', 'page'));
     if (empty($this->passedArgs['type'])) {
         $this->passedArgs['type'] = 'public';
         $projects = $this->Project->User->groups($this->Auth->user('id'));
         if (!empty($projects)) {
             $this->Session->write('Auth.User.ProjectPermission', $projects);
             $this->passedArgs['type'] = null;
             $this->paginate['conditions'] = array('Project.id' => array_keys($projects));
             $this->paginate['order'] = 'Project.private DESC, Project.url ASC';
         }
     }
     if (!empty($this->passedArgs['type'])) {
         $this->paginate['conditions'] = array('Project.private' => 0, 'Project.active' => 1, 'Project.approved' => 1);
         if ($this->passedArgs['type'] == 'forks') {
             $this->paginate['conditions']['Project.fork !='] = null;
             $this->paginate['order'] = 'Project.url ASC';
         } else {
             if ($this->passedArgs['type'] == 'public') {
                 $this->paginate['conditions']['Project.fork ='] = null;
                 $this->paginate['order'] = 'Project.url ASC';
             }
         }
     }
     if (User::get('id') == 1) {
         unset($this->paginate['conditions']);
         $this->paginate['order'] = 'Project.private ASC, Project.url ASC';
     }
     $this->Project->recursive = 0;
     $projects = $this->paginate();
     $this->set('projects', $projects);
     $this->set('rssFeed', array('controller' => 'projects'));
 }
All Usage Examples Of Router::connectNamed