CakePHP routing with colon separator - cakephp

I need to create routes that include a colon to produce URLs like http://app.com/prjct:a9b5c. Obviously it's currently simple to use a slash instead with the default routing.
$SLUG = array('slug' => '[-_A-Za-z0-9]+');
Router::connect('/prjct/:slug', array('controller' => 'projects', 'action' => 'show'), $SLUG);
But routes specifications use the colon character as a special indicator, which interferes with my naive attempt to replace the second slash above with another colon.
How do I use colons in this case for a route?

You can use named parameter as explained in CakePHP Cookbook. Write code below in your app/config/routes.php:
// Parse only the 'prjct' parameter if the current action is 'show' and the controller is 'projects'.
Router::connectNamed(array('prjct' => array('action' => 'show', 'controller' => 'projects')));
// Then set default route to controller 'projects' and action 'show
Router::connect('/', array('controller' => 'projects', 'action' => 'show'));
In your projects_controller.php :
function show(prjct = null) {
// Check if prjct match the pattern
$pattern = '[-_A-Za-z0-9]+';
if(!preg_match($pattern, prjct)){
// Redirect somewhere else
}
// Rest of your code here
}

I think this is indeed out of scope for simple routes. I see two options:
Use a custom route parsing class, as described here. There isn't a whole lot of documentation on the topic, but you can extend the existing class and play around with it to get a hang of what it's doing. Then customize it to your needs.
class MyRoute extends CakeRoute {
public function parse($url) {
debug($url); // input
$route = parent::parse($url);
debug($route); // output
return $route;
}
}
Route these URLs with a catch-all route to a controller, where the parameter will be available as a named parameter in $this->params['named']. Do what you need to do there.

Related

Cakephp Routing to generate URL parameter

Simple question for Cakephp 2.0.
I want to set a routing rule such that:
www.abc.com/z/abc123
will resolve to the full URL of (including the URL parameter)
www.abc.com/bookings/bookingref/?ref=abc123
Where bookings is the Controller, and bookingref is the action.
Can someone teach me what I need to write in the routes.php?
Kevin
In routes.php:
Router::connect('/bookingref/', array('controller' => 'bookings', 'action' => 'bookingref'));
In controller:
public function bookingref(){
}
So you should have a view name after your function. i.e. bookingref.ctp
This is how I would implement your solution:
In Config/routes.php add:
Router::connect('/z/:reference',
['controller' => 'bookings', 'action' => 'bookingref'],
[
'pass' => ['reference'],// Passed to corresponding function argument (order matters if 2 or more)
'reference' => '[a-z0-9]+'// RegExp validation if you need it
]
);
In your BookingsController use:
public function bookingref($reference = null)
{
...
}
Unfortunately, Router::redirect() cannot redirect to string based URLs that include variables. The controller based approach Progredi mentioned is your best bet.

CakePHP3.x controller name in url when using prefix routing

I am trying to use prefix routing in CakePHP3. I added the following lines to /config/routes.php.
Router::prefix("admin", function($routes) {
    // All routes here will be prefixed with ‘/admin‘
    // And have the prefix => admin route element added.
    $routes->connect("/",["controller"=>"Tops","action"=>"index"]);
    $routes->connect("/:controller", ["action" => "index"]);
    $routes->connect("/:controller/:action/*");
});
After that, I created /src/Controller/Admin/QuestionsController.php like below.
<?php
namespace App\Controller\Admin;
use App\Controller\AppController;
class QuestionsController extends AppController {
public function index() {
//some code here
}
}
?>
Finally I tried to access localhost/app_name/admin/questions/index, but I got an error saying, Error: questionsController could not be found. However, when I capitalize the first letter of controller name(i.e. localhost/app_name/admin/Questions/index), it is working fine. I thought it is weird because without prefix, I can use controller name whose first character is not capitalized.
Is this some kind of bug?
In Cake 3.x, routes do not inflect by default anymore, instead you'll have to explicitly make use of the InflectedRoute route class, as can for example bee seen in the default routes.php app configuration:
Router::scope('/', function($routes) {
// ...
/**
* Connect a route for the index action of any controller.
* And a more general catch all route for any action.
*
* The `fallbacks` method is a shortcut for
* `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);`
* `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);`
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$routes->fallbacks();
});
You custom routes do not specify a specific route class, so the default Route class is being used, while the fallback routes make use of inflected routing, and that is why it's working without prefix.
So either use capitalized controller names in your URL, or use a route class like InflectedRoute that transforms them properly:
Router::prefix('admin', function($routes) {
// All routes here will be prefixed with ‘/admin‘
// And have the prefix => admin route element added.
$routes->connect(
'/',
['controller' => 'Tops', 'action' => 'index']
);
$routes->connect(
'/:controller',
['action' => 'index'],
['routeClass' => 'InflectedRoute']
);
$routes->connect(
'/:controller/:action/*',
[],
['routeClass' => 'InflectedRoute']
);
});
See also http://book.cakephp.org/3.0/en/development/routing.html#route-elements

CakePHP 2.x - Custom Routes

I have two controllers: ArtistsController and RecordsController
I want to order routes logically depending on what the user is doing.
In this case once the user is editing an Artist (/artists/edit/some-artist) he's able to add some records for that artist.
So, I'd like the route to be something like:
"/artists/edit/some/artist/records/add"
And the same thing with the editing function of a record:
"/artists/edit/some-artist/records/edit/some-record"
I've been fighting with it for a while but I've never worked with Routes before on CakePHP and can't find a solution for this. Is this possible? Thanks
In Config/routes.php
Router::connect('/artists/edit/:some_artist', array('controller' => 'artists', 'action' => 'edit'), array('pass' => array('some_artist')));
Router::connect('/artists/edit/:some_artist/:records', array('controller' => 'artists', 'action' => 'edit'), array('pass' => array('some_artist','records')));
and you go on depending how what parameters you want to pass.
rule is simple: is some variable needs to be passed you put colon ":" before it, and add its name in array 'pass'.
I suggest read Routing: Route elements if you want specify type of passing element.
Additionally Artists Controller function should like this
public function edit($some_artist=null,$records = null) {
/**
[...]
*/
}

Cakephp route matches everything

I have following route added to routes.php in the end.
Router::connect('/:sellername/:itemtitle',
array('controller' => 'items', 'action' => 'view_item_detail'),
array(
'pass' => array('sellername','itemtitle'),
'sellername' => '[a-zA-Z0-9_-]+',
'itemtitle' => '[a-zA-Z0-9_-]+',
)
);
So this matches the dynamic urls like http://example.com/john/title-of-an-item
Problem is this also matches every other url like http://example.com/members/signin even though there's a MembersController controller and signin action in it.
I can fix it using following route entry.
Router::connect(
'/members/:action',
array('controller' => 'members')
);
But it's very tedious to add every route like above.
Doesn't existing matching controller names are prioritized while making a match?
Do order of routes in routes.php matter?
Custom Route classes is to help you
Custom route classes allow you to extend and change how individual routes parse requests and handle reverse routing. A route class should extend CakeRoute and implement one or both of match() and/or parse(). parse() is used to parse requests and match() is used to handle reverse routing.
You can use a custom route class when making a route by using the routeClass option, and loading the file containing your route before trying to use it:
Router::connect(
'/:slug',
array('controller' => 'posts', 'action' => 'view'),
array('routeClass' => 'SlugRoute')
);
This route would create an instance of SlugRoute and allow you to implement custom parameter handling.
custome routing class let you impliment anything
But personal opinion is to user a static and meaning full text in the url that diffrenciate it from the rest.

Adding a prefix to every URL in CakePHP

What's the cleanest way to add a prefix to every URL in CakePHP, like a language parameter?
http://example.com/en/controller/action
http://example.com/ru/admin/controller/action
It needs to work with "real" prefixes like admin, and ideally the bare URL /controller/action could be redirected to /DEFAULT-LANGUAGE/controller/action.
It's working in a retro-fitted application for me now, but it was kind of a hack, and I need to include the language parameter by hand in most links, which is not good.
So the question is twofold:
What's the best way to structure Routes, so the language parameter is implicitly included by default without having to be specified for each newly defined Route?
Router::connect('/:controller/:action/*', ...) should implicitly include the prefix.
The parameter should be available in $this->params['lang'] or somewhere similar to be evaluated in AppController::beforeFilter().
How to get Router::url() to automatically include the prefix in the URL, if not explicitly specified?
Router::url(array('controller' => 'foo', 'action' => 'bar')) should return /en/foo/bar
Since Controller::redirect(), Form::create() or even Router::url() directly need to have the same behavior, overriding every single function is not really an option. Html::image() for instance should produce a prefix-less URL though.
The following methods seem to call Router::url.
Controller::redirect
Controller::flash
Dispatcher::__extractParams via Object::requestAction
Helper::url
JsHelper::load_
JsHelper::redirect_
View::uuid, but only for a hash generation
Out of those it seems the Controller and Helper methods would need to be overridden, I could live without the JsHelper. My idea would be to write a general function in AppController or maybe just in bootstrap.php to handle the parameter insertion. The overridden Controller and Helper methods would use this function, as would I if I wanted to manually call Router::url. Would this be sufficient?
This is essentially all the code I implemented to solve this problem in the end (at least I think that's all ;-)):
/config/bootstrap.php
define('DEFAULT_LANGUAGE', 'jpn');
if (!function_exists('router_url_language')) {
function router_url_language($url) {
if ($lang = Configure::read('Config.language')) {
if (is_array($url)) {
if (!isset($url['language'])) {
$url['language'] = $lang;
}
if ($url['language'] == DEFAULT_LANGUAGE) {
unset($url['language']);
}
} else if ($url == '/' && $lang !== DEFAULT_LANGUAGE) {
$url.= $lang;
}
}
return $url;
}
}
/config/core.php
Configure::write('Config.language', 'jpn');
/app_helper.php
class AppHelper extends Helper {
public function url($url = null, $full = false) {
return parent::url(router_url_language($url), $full);
}
}
/app_controller.php
class AppController extends Controller {
public function beforeFilter() {
if (isset($this->params['language'])) {
Configure::write('Config.language', $this->params['language']);
}
}
public function redirect($url, $status = null, $exit = true) {
parent::redirect(router_url_language($url), $status, $exit);
}
public function flash($message, $url, $pause = 1) {
parent::flash($message, router_url_language($url), $pause);
}
}
/config/routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/:language/', array('controller' => 'pages', 'action' => 'display', 'home'), array('language' => '[a-z]{3}'));
Router::connect('/:language/pages/*', array('controller' => 'pages', 'action' => 'display'), array('language' => '[a-z]{3}'));
Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}'));
This allows default URLs like /controller/action to use the default language (JPN in my case), and URLs like /eng/controller/action to use an alternative language. This logic can be changed pretty easily in the router_url_language() function.
For this to work I also need to define two routes for each route, one containing the /:language/ parameter and one without. At least I couldn't figure out how to do it another way.
rchavik from IRC suggested this link: CakePHP URL based language switching for i18n and l10n internationalization and localization
In general, it seems that overriding Helper::url might be the solution.
An easier way might be to store the chosen language in a cookie and then not have to rewrite all the URLs. You could also potentially detect the user's browser language automatically.
However, search engines would be unlikely to pickup the various languages and you'd also lose the language if someone tried to share the link.
But love the full solution you posted, very comprehensive, thanks. :-)

Resources