CakePHP add default routing extension - cakephp-2.0

I've got a JSON REST api working for my application at a URL like: website.com/model/data.json using the CakePHP REST Simple Setup.
But I'd like it to also work the same even if I don't use the .json extension (in other words the response from website.com/model/data.json should be the same as website.com/model/data).
Is there any way to set an implied/default extension for a given controller function such that this would work?
FWIW: I'm using CakePHP 2.3.

To set up a route in CakePHP, add the following to your routes.php file:
Router::connect(
'/model/data',
array('controller' => 'model', 'action' => 'data', 'ext' => 'json')
);
See http://book.cakephp.org/2.0/en/development/routing.html for more details on routing.

Related

MissingRouteException in test of Table Object

I have a TableObject that sends an email in the afterSave-Event to notify the admin of a change. After the switch from cakephp-3 to cakephp-4 the test for this fails with this error message:
Cake\Routing\Exception\MissingRouteException: A route matching ... could not be found.
The exception occurs on a line in the email template where I build a link like this:
$this->Url->build([
'prefix' => 'Partner',
'controller' => 'orders',
'action' => 'view',
$order->id,
]);
I believe that the routes are not set up in the context of a test for a Tableobject and therefore the reverse routing is not working. (I only get the error when running the test, not when the email is sent in the app).
Is there a way I can load all routes in the test?
Since 4.x the routes are not auto loaded anymore.
You need to add
$this->loadRoutes();
into your setUp() or before the test run to actively load them now.

Cakephp 2.6 how do I return to the main application after entering a plugin

I am using Luis Dias' Report Manager Plugin to generate some quick reports for an application I am developing. From my application dashboard I enter the plugin using the following:
<td style="text-align:center">
<button style="height:75px; width:175px; background-color:BurlyWood; font-size:20px; font-family:Verdana"
onclick="window.location.href='<?php echo Router::url(array('controller'=>'report_manager'
,'action'=>'reports'))?>'">Report Management</button>
I'd really like to open the Report Generator Wizard in a new window but that's a different issue..
Once I am done with the report generator I'd like to return to my dashboard in my application. However, I am now in the Plugin's domain and can't figure out a command to "route" me back to the calling application.
Thanks in advance
Mike
To 'escape' from a plugin when routing you need to pass plugin => false in the route array. For example:-
$this->Html->url([
'controller' => 'pages',
'action' => 'view',
1,
'plugin' => false
]);
If you don't pass the plugin attribute it assumes you want to remain in the context of the current plugin. You need to be careful with this wherever you use links where plugins are in use.

CakePHP Auth Module Tutorial--Why is it requesting user/login for these pages?

I have a custom routing setup (which might be cause of issue). As follows:
Router::connect('/o/*', array('controller' => 'open', 'action' => 'openinsert'));
Router::connect('/c/*', array('controller' => 'click', 'action' => 'clickinsert'));
Under UsersController, I have tried:
public function beforeFilter() {
parent::beforeFilter();
//$this->Auth->allow('add');
$this->Auth->allow('o/*');
$this->Auth->allow('clickinsert');
$this->Auth->allow('open');
}
clickinsert and openinsert are both apart of controller 'ClickController' and 'OpenController' respectively.
Nothing from above works. Is is possible to do what I'm wanting to do w/their default auth setup? Or will I need to make some major changes?
AuthComponent::allow
This method:
Takes a list of actions in the current controller for which authentication is not required, or no parameters to allow all actions.
It doesn't accept urls.
In particular this:
$this->Auth->allow('o/*');
Is neither the wildcard ( nothing or '*' depending on the version of CakePHP used) nor the name of an action, and this:
$this->Auth->allow('open');
Is apparently passing the name of a controller so neither of those method calls would do anything.
There's some confusion here
According to these two routes:
Router::connect('/o/*', array('controller' => 'open' <-
Router::connect('/c/*', array('controller' => 'click', <-
They point to two different controllers.
Under UsersController, I have tried:
Anything you put in your users controller is irrelevant for requests that don't use it. Either put the allow calls in the before filter for each controller - or put them in the beforeFilter for your App controller (which all controllers inherit from).

add language prefix to URL from cookie on page load in CakePHP

I'm quite new on cakePHP. I'm creating multilanguage page using this tutorial: i18n multilanguage tutorial everything is working fine, but on page load I need to add language prefix from cookie (localhost/eng instead of localhost/), this prefix appears when I select some menu, but I had a headache how to add prefix on pageload. Thanks for advices.
You can do a redirect in your AppController, after you call _setLanguage(). Something like:
$this->_setLanguage();
if( $this->here == '/' )
$this->redirect(array('controller' => 'your_controller', 'action' => 'your_action', 'language' => $this->Session->read('Config.language')));

cakephp why can't I have an admin route and a superuser route?

In core.php I can define
Configure::write('Routing.admin', 'admin');
and /admin/controller/index will work.
but if I define both
Configure::write('Routing.admin', 'admin');
Configure::write('Routing.superuser', 'superuser');
and try to look at /superuser/blah/index/ instead of it saying the controller doesn't exist it says
Error: SuperuserController could not be found.
instead of saying
Error: BlahController could not be found.
When I first read the documentation I was under the impression I could run both routes, and not just one or the other. Is there something more I need to do?
I believe they are working on this for CakePHP 1.3, but for now, we have to cheat to accomplish additional routing. This is the method I've used in the past.
// SuperUser Routing
Router::connect('/superuser/:controller',
array('prefix' => 'superuser', 'superuser' => true));
Router::connect('/superuser/:controller/:action/*',
array('prefix' => 'superuser', 'superuser' => true));
There were some issues generating URLs using the array('controller' => ...) method, but I haven't touched that project in a few months, so I can't remember all the caveats with it. This should at least give you a starting point though.
The CakePHP document explains this some. The relevant section starts about halfway in talking about multiple prefixes.
If you're using Jason's trick, and having trouble with generating URLs using the array('controller' => ...) syntax, then put this in your appcontroller:
if (isset($this->params['prefix']) && $this->params['prefix'] == 'superuser') {
Configure::write('Routing.admin', 'superuser');
}
This forces the appcontroller to use the correct admin prefix, which in this case is "superuser".

Resources