PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] How to connect routes that start with custom elements?

 April 22, 2022     cakephp, cakephp-2.3     No comments   

Issue

I want to place a named parameter before the controller/action and everything else that comes after...

e.g:

domain.com/named_parameter/controller
domain.com/named_parameter/controller/action
domain.com/named_parameter/controller/action?key=val
domain.com/named_parameter/controller/action/other_params

I don't care how the rest of the URL will be structured after the named_parameter. I just want to place the parameter in front :)

And, I also need to make the standard URLs without the named parameter in front work.

e.g:

domain.com/controller
domain.com/controller/action
domain.com/controller/action?key=val
domain.com/controller/action/other_params

To make it simple, the named parameter will only be matched if it starts with foo_

e.g:

domain.com/foo_biz/controller/action
domain.com/foo_zip/controller/action

Is the matching possible? And if yes, how?


Solution

It's all described in the Cookbook, see

  • http://book.cakephp.org/2.0/en/development/routing.html#connecting-routes
  • http://book.cakephp.org/2.0/en/development/routing.html#route-elements

It doesn't seem like you are atually talking about named parameters (removed as of CakePHP 3.0, so it's better to stop using them already today), as they are in name:value format, but simply about ordinary path components.

You can easily define your own route element and use the regex matching option to make sure the route connects only to URLs where the element starts with foo_. Make use of the :controller and :action elements and your routes will connect to the same controllers/actions as the ones without the foo_ path component.

Place the routes before your other ones so that they take precedence and you should be good.

$options  = array(
    'store' => 'foo_[^\/]+', // matches everything that starts with `foo_`
    'persist' => array('store')
);

/**
 * Connects:
 *
 * /foo_storename/controller/action
 * /foo_storename/controller/action/whatever/etc...
 * /foo_storename/controller/action?bar=baz&etc...
 */
Router::connect(
    '/:store/:controller/:action/*',
    array(),
    $options
);

/**
 * Connects:
 *
 * /foo_storename/controller
 * /foo_storename/controller?bar=baz&etc...
 */
Router::connect(
    '/:store/:controller',
    array('action' => 'index'),
    $options
);

/**
 * Connects:
 *
 * /foo_storename
 * /foo_storename?bar=baz&etc...
 */
Router::connect(
    '/:store',
    array('controller' => 'index', 'action' => 'index'),
    $options
);

store is then accessible on the request object in your controllers, magically as a property of the request object:

$this->request->store;

or explicitly in the params property:

$this->request->params['store'];


Answered By - ndm
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing