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')));
Related
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.
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.
I am developing a hrm panel in cakephp. I want when employee punchin the page redirect to punchout page,but if employee logout the panel then he goes to punchin page that page must not be redirected until it checks the boolean value from the database.
I disagree with the other answer because it does not show the best way to do it in CakePHP. If you use the router the code provided by the other answer will not work with routing. Also if your app is not in the root of the domain / but for example.com/my-tool/ the string type URL won't work either and the link is wrong, it would go to example.com/punchout instead of example.com/my-tool/punchout
if ($value === true) {
$this->redirect(array('controller' => 'employees', 'action' => 'punchout'));
}
This is the correct way to do links in CakePHP for all links that point to any controller of your application. The string should be only used if it is an external URL. If you use links in your layout and have plugins and routing prefixes you also want to add the plugin and prefix key to the array and set it according to your needs.
Might it be so simple just to put your redirect code into a IF statement?
if($boolean === true) {
$this->redirect(array('controller' => 'employees', 'action' => 'punchout'));
}
//normal code here when boolean is false
Hi The master CakePHP! I want to ask something.
I think the probem is so simple, Is it possible to generate a url in cakephp application like this? (This is google mail like url - http://mail.google.com/a/domain.com)
http://www.domain.com/a/[something]/[member]/[controller]/[action]
Where [something] is a dynamic variable, [member] is a prefix, [controller] and [action] is usual cakephp url element.
so I want to shift the usual CakePHP url to the right, or insert /a/something/ in the initial url.
In addition, [something] should be read from the controller action
thanks, your help would be very valuable to me and will really help me
QUESTION v2:
I have tried to add this routing into routes.php
Router::connect('/:code/:something/:controller/:action',
array(),
array('code'=>'a','pass'=>array('something'))
);
when i try to access a http://domain.local/a/pte/users/view, it's work, but it display an error when i try to access http://domain.local/a/pte/users
QUESTION v3:
So the simple question is:
How do I have a url like google (example above) without breaking the
default CakePHP routing?
QUESTION v4: (Thank's for #Rui)
I tried to create this routing:
Router::connect('/:code/:something/:prefix/:controller/*',
array(),
array('code'=>'a','pass'=>array('something'))
);
It's ok when i try to access
http://domain.local/a/pte/member/users (The result is cake render member_index view, It's a great progress :-) ),
but there are several issue:
when i try to access http://domain.local/a/pte/users (without prefix member, i decided to display index function) it's failed (display an error AController did not defined)
When i create a link
echo $html->link('test link',array('code'=>'a','something'=>'pte','member'=>true,'controller'=>'users','action'=>'another_view'));
it generate a link like this
http://domain.local/member/users/another_view/code:a/something:pte
Please try in /app/config/routes.php:
<?php
Router::connect('/a/:something/:member/:controller/:action/*');
?>
And the action should be:
<?php
public function action($something = null, $member = null) {
/* action code */
}
?>
Hope it helps.
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".