Custom route configuration in cakephp - cakephp

In route file there is a line like
Router::connect('/', array('controller' => 'admins', 'action' => 'login'));
I want to do something if anyone write a URL like http://abc.com/webroot or http://abc.com/css_or_js then it also goes to admin's login action. If so then what can i do then?
Router::connect('/webroot/*', array('controller' => 'admins', 'action' => 'login'));
Router::connect('/css/*', array('controller' => 'admins', 'action' => 'login'));
Router::connect('/js/*', array('controller' => 'admins', 'action' => 'login'));
but it works for webroot now and did not work for css or js folder or any other folder. Please help me in this matter. I will be very grateful to you.

The reason the css and js routes aren't working is because Cake's dispatcher is seeing them as an asset, so it skips the routing process entirely and delivers the asset. The only way around this, as I see, is to write a custom dispatcher.
You shouldn't be writing routes for the webroot directory and its folders anyway. The webroot folder should be the document root on your virtual host, and is therefore seen as the root of the site.

Related

How to access plugin in cakephp

I am new in cakephp, I want to call the plugin via the URL. Here is URL of
http://testproject.local/PluginName/ControllerName/ActionName
When I Run this URL at that time I found "Missing Controller" error.
Missing Controller
Error: <ControllerName>Controller could not be found.
Error: Create the class <ControllerName>Controller below in file: `app/Controller/<ControllerName>Controller.php`
It's showing me
`Exception Attributes: array ( 'class' => 'PracticeFusionController', 'plugin' => NULL, )`
Here is my routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/Pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/gods/:action/*', array('plugin' => 'nova', 'controller' => 'gods'));
Router::connect('/gods', array('plugin' => 'nova', 'controller' => 'gods'));
Router::parseExtensions('json', 'xml');
Router::mapResources('events');
Router::connect('/<pluginName>', array('plugin' => '<pluginName>', 'controller' => '<ControllerName>'));
You can only load/open Plugin routes if you actually loaded the plugin in your project's bootstrap. You don't need to include a plugin route in your core app's routes.php. If you want to add plugin specific routes, you can load the plugin specific routes file, using the routes option. Note that by default all /plugin/controller/action routes are already routed properly, you don't need a separate routes file for that.
So, in your core app's app/Config/bootstrap.php, add:
CakePlugin::load('YourPlugin', array('routes' = true));
The routes from your plugin's Config/routes.php will then be loaded and can be used.
More details on this can also be found in the documentation.
If above solutions does not works, please load plugin like CakePlugin::load('YourPlugin', array('routes' => true)); Please note array passed is : array('routes' => true)

How we can create a hyperlink in default.ctp, Am I create an other controller to redirect it or somthing else?

So please help me in this issue, how to give hyperlink in cakephp and my rout file is as following:
Router::connect('/', array('controller' => 'home', 'action' => 'home', 'display'));
Router::connect('View/pages/*', array('controller' => 'admin_login', 'action' =>'admin_login', 'home'));
my hyperlink is in default.ctp is :
href=admin_login.php
Is there something different from this
Use this one
echo $this->Html->link('Click', '/page/home');
if you want to use .html then add this line into routes.php
Router::parseExtensions('html');

Routing configuration in cakephp

I am trying to implement routing in cakephp. I want the urls to mapped like this...
www.example.com/nodes/main -> www.example.com/main
www.example.com/nodes/about -> www.example.com/about
So for this I wrote in my config/routes.php file..
Router::connect('/:action', array('controller' => 'nodes'));
Now, I got the thing going but when I click on the links, the url in browser appears like
www.example.com/nodes/main
www.example.com/nodes/about
Is there some way where I can get the urls to appear the way they are routed?
Setting in .htaccess or httpd.conf would be easy - but I don't have access to that.
Regards
Vikram
This should work:
Router::connect('/main', array('controller' => 'nodes', 'action' => 'main'));
Router::connect('/about', array('controller' => 'nodes', 'action' => 'about'));
You may also do something more powerful, like this:
$actions = array('main','about');
foreach ($actions as $action){
Router::connect('/$action', array('controller' => 'nodes', 'action' => '$action'));
}
Basically if your links are created with Html helper, with the following format:
<?php echo $this->Html->link('your link', array('controller'=>'nodes', 'action'=>'main'));?>
Then the Cake will convert the links properly to www.example.com/main
But if your links are
<?php echo $this->Html->link('your link', '/nodes/main/');?>
they will point to www.example.com/nodes/main

Cakephp, Route old google search results to new home page

I have created a new website for a company and I would like all the previous search engine results to be redirected. Since there were quite a few pages and most of them where using an id I would like to use something generic instead of re-routing all the old pages.
My first thought was to do that:
Router::connect('/*', array('controller' => 'pages', 'action' => 'display', 'home'));
And put that at the very end of the routes.php file [since it is prioritized] so that all requests not validating with previous route actions would return true with this one and redirect to homepage.
However this does not work.
When I use a different path on the Router it redirects successfully. For example if I give it:
Router::connect('/*', array('controller' => 'projects', 'action' => 'browser'));
it works fine. The problem arises when the controller used is pages, action display etc.
I'm pasting my routes.php file [since it is small] hoping that someone could give me a hint:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/company/*', array('controller' => 'articles', 'action' => 'view'));
Router::connect('/contact/*', array('controller' => 'contacts', 'action' => 'view'));
Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));
Router::connect('/eng/*', array('controller' => 'p28n', 'action' => 'shuntRequest', 'lang' => 'eng'));
Router::connect('/gre/*', array('controller' => 'p28n', 'action' => 'shuntRequest', 'lang' => 'gre'));
Router::parseExtensions('xml');
Instead of trying to handle everything within the cakePHP route file, I would recommend that you use the .htaccess file to 301 redirect pages as necessary.
What you have above will not transfer the rankings over because as far as i can see there is no 301 redirect being outputted in any of the routes.php based solutions you proposed.
The bigger problem is that a Route doesn't redirect, it connects URLs with responses. In other words, it makes sure that your now invalid URLs still yield a valid page. Which is exactly the opposite of what you want to achieve.
You want to tell visitors that a URL that used to be valid isn't anymore. You do this by issuing appropriate HTTP response codes, 301 Moved Permanently in this case. Without doing this the URLs will still appear valid to search engines and they won't update their index.
You'd either have to connect all the invalid URLs via Routes to some controller action that'll issue a $this->redirect('...', 301) or you could use some .htaccess rules to redirect. Which one to use depends on the complexity of the redirect, but you'll probably be able to use simple .htaccess mod_rewrite rules.
There are enough examples on SO: https://stackoverflow.com/search?q=htaccess+301+redirect

cakephp routing problem, plugin routing works but not others

I'm having a strange routing problem with a site I just uploaded, and I've made a number of changes to test what's happening. It doesn't make any sense.
My setup is:
I'm using one plugin, which I've included all the routing in the routes.php file.
I've also included the routes for two other controllers, 'events' and 'updates'
they look like this:
Router::connect('/login', array('plugin' => 'pippoacl', 'controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('plugin' => 'pippoacl', 'controller' => 'users', 'action' => 'logout'));
Router::connect( '/events/', array( 'controller' => 'events', 'action' => 'index'));
Router::connect('/updates', array('controller' => 'updates', 'action' => 'index'));
What happens when I try to get to 'events' is that I get an error message saying:
"Not Found
Error: The requested address '/Events' was not found on this server."
I've checked the database and it's accessible, through the plugin's model/controller/view.
I've also made sure the model/controllers for 'events' and 'updates' are there.
Can anyone tell me how to trouble shoot this?
Thanks,
Paul
Do you open /events or /Events? URL-s - except the domain part - are case sensitive.
Thanks Sibidiba,
As it turns out this happened because there was a user model and user controller in the application folder as well as the plugins controller. So the routing treated other controllers as if they weren't there.
All fixed now.
Cheers, Paul

Resources