cakephp - strange issue with routing - php

I have my app set up to go to Books index page
Here is my address
http://myapp.loc/
Here is router
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
When I go to http://myapp.loc/, the page loads up just fine. But when I click a link within that page that is suppose to link to http://myapp.loc/books/index, it doesn't work.
The web developer tools show
Remote Address:127.0.0.1:80
Request URL:http://myapp.loc/books/index
Request Method:GET
Status Code:404 Not Found
I tried adding a statement in books controller under index action
die("here is my breakpoint");
I see this when the app loads. But if I take it out and let the app load and then click on the link thats basically the same thing, it doesnt even hit that part of the code.
Any help will be appreciated.
Router
<?php
/**
* Here, we are connecting '/' (base path) to controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/View/Pages/home.ctp)...
*/
//Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
Router::mapResources('books', array('id'=>'[0-9A-Za-z]'));
Router::parseExtensions('json');
require CAKE . 'Config' . DS . 'routes.php';

Try to add second line like
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
Router::connect('/books/*', array('controller' => 'books', 'action' => 'index'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Check Manual Here

Related

NotFound in cakephp

I'm new to CakePHP, in this project, there's a route like
Router::connect('/', array('controller' => 'pages', 'action' => 'index'));
If I access http://localhost/, it will display the content of view
but if I change it to
Router::connect('/asdf', array('controller' => 'pages', 'action' => 'index'));
and access http://localhost/asdf, it will tell 404 not found.
I search a lot, and don't know why
sample code here:
https://github.com/openshift/cakephp-ex
Add this code in end of your routes.php file.
require CAKE . 'Config' . DS . 'routes.php';

Cakephp routes redirect base path

I am trying to make an URL like these:
www.website.com/,
www.website.com
redirect to
www.website.com/members/login
through routes.php.
I have this at the moment: Router::connect('/', array('controller' => 'home', 'action' => 'index'));
How can i setup the route / to reach my desired url?
Thank you!
You can just replace the line:
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
with:
Router::connect('/', array('controller' => 'members', 'action' => 'login'));
However, I believe you don't want to do this.
Just leave your routes untouched, and set up AuthComponent properly:
class AppController extends Controller {
// Pass settings in $components array
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'members',
'action' => 'login',
),
//[...] rest of your Auth options
)
);
For further reference, see Cookbook 2.x: Authentication.
Probably you have AuthComponent somewhere and '/' is set as dissallowed.
$this->Auth->allow('/') should help.

Admin Routing in cakephp

Im trying to put my app under /admin on cakephp. And i am trying to configure the admin routing. What im trying to achive is this:
Lets say the page is www.example.com so when the user type www.example.com/admin i want him/her to be redirected to the admin_dashboard.ctp (if it is logged in, otherwise redirect to log-in page). But now the problem is when i type www.example.com/admin it shows an error like:
Action PagesController::admin_index() could not be found
but if i do:
www.example.com/admin/users/dashboard it is redirected properly.
How can i achieve that? so just by typing /admin to redirect to dashboard??
and another thing is it possible to remove /users/ from url and just display admin/dashboard?
On core.php file i have added the following line:
Configure::write('Routing.prefixes', array('admin'));
And on the routes.php i have these lines :
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard', 'dashboard'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/* I added this line for admin routing */
Router::connect('/admin', array('controller' => 'pages', 'action' => 'index', 'admin' => true));
In routes.php write
Router::connect('/admin', array('controller' => 'users', 'action' => 'dashboard', 'admin' => true));
instead of
Router::connect('/admin', array('controller' => 'pages', 'action' => 'index', 'admin' => true));
This redirect you to admin_dashboard when you type www.example.com/admin.
Routing prefixes are added to the action name when looking for its corresponding method in the Controller.
In the Pages Controller rename your index() method into admin_index() as suggested by the error you are getting.

How to connect to a plugin route on routes.php file CAKEPHP

I have a plugin named Manager. I want to map route on /login to the index action of Dashboard Controller on plugin. Something like this:
Router::connect('/login', array('controller' => 'dashboard', 'action' => 'index', 'plugin => 'manager'));
How can I achieve this on Cakephp 2.2?
thanks
Once a plugin has been installed in /app/Plugin, you can access it at the URL /plugin_name/controller_name/action. In your Manager plugin example, you'd access DashboardController at /manager/dashboard.
Have you tries using Plugin Syntax, like:
Router::connect('/login',
array('controller' => 'Manager.dashboard', 'action' => 'index')
);
Using the 'plugin' key in the $options array should also do the job:
Router::connect('/login',
array('plugin' => 'manager','controller' => 'dashboard', 'action' => 'index')
);
If use plugin like 'users plugin' try in plugin's routes file (routes.php)

Link for static page in CakePHP without appending the controller

So I created a couple of static pages in views > pages folder. They are contact.ctp and privacy.ctp. In my routes.php, I made it so that they could be viewed by going to domain.com/contact and domain.com/privacy with:
Router::connect('/contact', array('controller' => 'pages', 'action' => 'display', 'contact'));
Router::connect('/privacy', array('controller' => 'pages', 'action' => 'display', 'privacy'));
Now, when I link them at the footer with:
<li><?= $this->Html->link('Contact', array('controller' => 'pages', 'action' => 'display', 'contact')); ?></a></li>
<li><?= $this->Html->link('Privacy', array('controller' => 'pages', 'action' => 'display', 'privacy')); ?></a></li>
They are linked as domain.com/pages/terms. How can I stop it from appending the pages controller without giving an absolute url (i.e. without doing: <?= $this->Html->link('Contact', 'http://www.domain.com/contact'); ?> or is that the only other way?
you probably put these routes after Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); Just reverse that order and it should work.
ROUTE
Router::connect('/*', array('controller' => 'pages', 'action' => 'display'));
VIEW
echo $this->Html->link('Target', $this->Html->url(array('controller'=>'pages', 'action'=>'display', 'target', 'ext'=>'html')));
OUPUT
Target
Use an actual link?
Contact
And:
Privacy
Short and sweet ^_^
For SE posterity, and the sake of succinctness, you can use Router::url( ).
<li>Contact</li>

Categories