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';
Related
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.
I am using cake php and due to some reason i want to hide controller and action name from the url . current url us like
http://192.168.1.31/home/this_is_test
where home is controller name and this_is_test is slug which is dynamic . i want the url like
http://192.168.1.31/this_is_test.
my routes.php is
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect('/dashboard', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
Router::connect('/home/*', array('controller' => 'Home', 'action' => 'index'));
I have read a couple of solution after googling . also tried this in routes.php . but no luck
Router::connect(
'/:query',
array('controller' => 'Home', 'action' => 'index',1),
array('query' => '[a-zA-Z]+')
);
anybody have idea about this if it is possible??
Your solution
For static text try this:
Router::connect('/this_is_test', array(
'controller' => 'home',
'action' => 'this_is_test OR any_other action name'
));
If it's dynamic
Router::connect('/:id',
array('controller' => 'home', 'action' => 'index'),
array(
'pass' => array('id'),
array('id' => '[A-Za-z]')
)
);
References: Cakephp2.x Route
I hope I knew what you really want to achieve. You can place the Route in the last position. Here is the reference .
Other option would be to use alias for your controller. So you call your controller some thing else and set a new name for your controller then call it in you Route.
If this doesn't work then you would need to write a bespoke Component in order to help you to do that.
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
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.
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)