I have the this route defined in routes.php
$route['user'] = "user_controller";. The user controller has a method logout() but when I try this URI user/logout I get a 404. In the same way when I use this URI user/index I get a 404.
routes.php
// custom routes
$route['start'] = "start_controller";
$route['register'] = "register_controller";
$route['user'] = "user_controller";
// other routes
$route['default_controller'] = "start_controller";
$route['404_override'] = '';
According to CI
Note: Routes will run in the order they are defined. Higher routes
will always take precedence over lower ones.
$route['default_controller'] and $route['404_override'] must always be on top above others
$route['user/logout'] = "user_controller/logout";
$route['user/index'] = "user_controller";
Example i will type a user/logout then it will proceed to user_controller/logout you have to define the URL you would like to redirect
Yep, you have to specify a route for each particular method.
Here's an example from my routes.php :
/* User Authentication Controller */
$route['login'] = "auth/login";
$route['logout'] = "auth/logout";
$route['register'] = "auth/register";
$route['forgot'] = "auth/forgot";
Related
I have created a multi-language website.
Facing issue while creating SEO user-friendly URL.
current URL:
http://localhost/example/en/user/myaccount OR
http://localhost/example/fr/user/myaccount
want to change it with
http://localhost/example/en/myaccount OR
http://localhost/example/fr/user/myaccount
routes.php
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// URI like '/en/about' -> use controller 'about'
$route['^(en|de|fr|nl|id|es)/(.+)$'] = "$2";
// '/en', '/de', '/fr','/es' and '/nl' URIs -> use default controller
$route['^(en|de|fr|nl|id|es)$'] = $route['default_controller'];
also tried with
$route["myaccount"] = "user/myaccount";
$route["^(en|de|fr|nl|id|es)$/myaccount"] = "user/myaccount";
Nothing will work in this case. Already used following in routes.php file other demo projects with out multi-language. There it's work perfectly.
$route["myaccount"] = "user/myaccount";
Thanks in advance
you can consult the documentation for routes here
https://www.codeigniter.com/user_guide/general/routing.html
and for your problem
$route["(:any)/myaccount"] = "user/myaccount/$1";
I'm having some troubles with routing in codeigniter.
My routing file is as below:
$route['admin/newgallery'] = 'gallery/do_upload';
$route['admin/listgallery'] = 'gallery/list';
$route['admin/create'] = 'posts/create';
$route['admin/listposts'] = 'posts/list';
$route['admin'] = 'admin/index';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['default_controller'] = 'pages/index';
$route['(:any)'] = 'pages/index/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
All routes work just fine except for the first two:
$route['admin/newgallery'] = 'gallery/do_upload';
$route['admin/listgallery'] = 'gallery/list';
When I type mypage/admin/listgallery it calls gallery/list correctly. The problem is when I type the address with the original controller/method (in this case gallery/list) it goes to the list page as well when it should call a 404 error! Every other routing rule I have set does that, except the first two!
Out of the Box, Codeigniter allows you to directly access any Controller/Method from the URL.
It also provides the creation of custom routes so you could have 10 or more urls all pointing at the same controller/method with parameter passing if that was your desire...
So in the case you ONLY want access to any Controller/Method that are defiend in the Routes config.
You need to test if the url is defined in the routes config array.
The main code is something like...
$this->load->helper('url');
if(!isset($this->router->routes[uri_string()])){
show_404(); // Or whatever you want ...
}
And you would put this in your controller's constructor you want to protect.
Of course you could create a common controller and extend those controllers you want to protect in this manner.
( NOT Recommended ) Or if you want to get really "hacky" you could put it in the system/core/controller constructor and make it system wide. SO Everything needs to be defined in a route.
NOTE: This breaks the 'default_controller'.
I have these routes :
$route['default_controller'] = 'Pages/view/$1';
$route['product/(:any)'] = 'pages/product/$1';
$route['category/(:any)'] = 'pages/category/$1';
$route['(:any)'] = 'pages/view/$1';
but when I want to add $route['404_override'] = 'My404' my any routing stops working and I'm not able to get to my homepage like base_url('home')...
you need to define the route for home controller. Problem is that you have defined $route['(:any)'] = 'pages/view/$1'; which will override all undefined routes. So either you need to change the $route['(:any)'] = 'pages/view/$1'; to something else or need to define the route for home like: $route['home'] = 'home_controller';
exmple: this load default controller/class with function page,
www.example.com/page
unless we have controller/class named page AND set $route['page'] = 'page'; it'll load the controller. But if we dont set the $route, it'll still load default_controller.
is that true a controller must have a $route[''] always? is not it possible to load controller page without set $route[''] even there is no default controller function with same name?
Edit:
I access
www.mysite.com/index.php/user
I do have user controller with index function, but my route file only contain:
$route['default_controller'] = 'page';
$route['(:any)'] = 'page/$1';
$route['product'] = 'product';
//$route['user'] = 'user';
$route['404_override'] = '';
returns 404, only works if I uncomment this: $route['user'] = 'user';
why?
Thanks.
No, that's not true. CodeIgniter, by default, directly maps URI segments to:
example.com/index.php/controller/method/param/param/...
Or if you have an .htaccess / similar solution to remove index.php:
example.com/controller/method/param/param/...
Routing is used when you wish to use a URL that does not directly map to this convention.
Edit: You have conflicting routes. CodeIgniter will look at each route in order from top to bottom, and if it find one that matches, it stops looking and processes that route. Because you have an (:any) catch-all route, it will match anything (like it says).
The rule of thumb is to place your most specific routes first, and then get more generic and catch-all later. Your (:any) route should be the very last one in your list. And the default controller and 404 overrides should stay first.
$route['default_controller'] = 'page';
$route['404_override'] = '';
$route['product'] = 'product';
$route['user'] = 'user';
$route['(:any)'] = 'page/$1';
You need to add the product and user routes because you've defined the (:any) route. If you want to avoid writing route rules for every one of your existing controllers, but still take advantage of a catch-all controller, consider using the 404_override controller/method instead. You can do your verifications to check if the URI is valid there. Just make sure to throw a 404 error if not (you can use show_404()), since any non-existant URL will be routed to there.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
CodeIgniter - When using $route[‘(:any)’] = ‘pages/view/$1’ how to use other controllers?
I'm working on a url shortener/redirect web app in codeigniter and have ran into an issue with my routes.
The first route i have is:
$route['(:any)'] = "redirect/index/$1";
This is what handles the redirect, so example.com/dwB would goto the redirect controller.
Underneath I have a few routes that override that for pages and other controllers etc. They work fine when i describe the entire url including arguments etc but I have now ran into trouble as i have some dynamic urls like:
example.com/stats/view/dwB
Or facebook auth responses etc, which i can't obviously write a route for. I've tried using something like:
$route['stats/view/(:any)'] = "stats/view/$1";
but none of these appear to override the first route. Here is my entire routes.php document.
$route['(:any)'] = "redirect/index/$1";
$route['shorten/create'] = "shorten/create"; // overwrite the previous route
$route['stats/view/(:any)'] = "stats/view/$1"; // allow the stats controller to be used
$route['login'] = "auth/login"; // allow the login(auth) controller to be used
$route['register'] = "auth/register"; // allow the login(auth) controller to be used
$route['auth_social/fblogin'] = "auth_social/fblogin"; // allow the login(auth) controller to be used
$route['dashboard'] = "dashboard"; // allow the login(auth) controller to be used
$route['auth/logout'] = "auth/logout"; // allow the login(auth) controller to be used
$route['auth'] = "auth/index"; // allow the login(auth) controller to be used
$route['default_controller'] = "pages";
$route['404_override'] = '404';
As I mentioned in the comments, place your "any" route at the end of your custom routes, like this:
$route['shorten/create'] = "shorten/create"; // overwrite the previous route
$route['stats/view/(:any)'] = "stats/view/$1"; // allow the stats controller to be used
$route['login'] = "auth/login"; // allow the login(auth) controller to be used
$route['register'] = "auth/register"; // allow the login(auth) controller to be used
$route['auth_social/fblogin'] = "auth_social/fblogin"; // allow the login(auth) controller to be used
$route['dashboard'] = "dashboard"; // allow the login(auth) controller to be used
$route['auth/logout'] = "auth/logout"; // allow the login(auth) controller to be used
$route['auth'] = "auth/index"; // allow the login(auth) controller to be used
// Move "any" route down here...
$route['(:any)'] = "redirect/index/$1";