Overriding routes in codeigniter [duplicate] - codeigniter

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";

Related

Codeigniter 3.1.16 routing issues

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'.

`translate_uri_dashes` not working codeigniter route

i already tried this code in route file but it not convert _ to - by default
$route['translate_uri_dashes'] = TRUE;
$route['stock/upload_stock'] = 'stock/upload_stockt';
CodeIgniter 3 provides a nice way for it ,There is a route
$route['translate_uri_dashes'] = false;
which is by default set to false, but if you set it to true, you can name your controllers and controller methods using the underscores (_s) and can call them using dashes (-s).
For example you have a controller named Company, and inside your Controller you have a method called about_us, now you can call it both the ways /company/about_us and company/about-us , when your $route['translate_uri_dashes'] is set to true.
So, try making your route as below
$route['stock/upload-stock'] = 'stock/upload_stock';

Codeigniter minimize URLs with extending controller and multilingual

I've configured Codeigniter 2.1 with i18n and extended controller.
I've hide the main controller "main" and I kept visible "admin" and "blog" controller.
This urls works fine:
www.mysite.com/ en / functionname
www.mysite.com/ en / blog /
This is my problem:
www.mysite.com/ it / blog / functionname
With the main controller "blog" everything after "/" is ignored.
Is it possible to do this?
My routes.php file:
$default_controller = "main";
$language_alias = array('it','en');
// exceptions
$controller_exceptions = array('admin','blog');
// route
$route['default_controller'] = $default_controller;
$route["^(".implode('|', $language_alias).")/(".implode('|', $controller_exceptions).")(.*)"] = '$2';
$route["^(".implode('|', $language_alias).")?/(.*)"] = $default_controller.'/$2';
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';
foreach($language_alias as $language) {
$route[$language] = $default_controller.'/index';
}
$route['404_override'] = '';
// URI like '/en/about' -> use controller 'about'
$route['^(it|en)/(.+)$'] = "$2";
// '/it', '/en' URIs -> use default controller
$route['^(it|en)$'] = $route['default_controller'];
If I remove lang in my URL everything works fine:
www.mysite.com/ blog / functionname
I think you need another segment in your routes for accessing the controllers function.
so you'll need a second line:
// '/it', '/en' URIs -> use default controller
$route['^(it|en)$'] = $route['default_controller'];
// URI like '/en/about' -> use controller 'about'
$route['^(it|en)/(.+)$'] = "$2";
// URI like '/en/about/test' -> use controller 'about' with function 'test'
$route['^(it|en)/(.+)/(.+)$'] = "$2/$3";
I don't use the i18n or support multiple language in any project atm so I can't test but this should do the trick.

CodeIgniter: Understanding routes

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";

How to set url for particular controller action - Magento

If I have module mymodule in which I have index Controller. In which I have subaction as 'subaction'
Normally I access page as
http://www.mywebsite/index.php/mymodule/index/subaction
How can I set url from code such as
http://www.mywebsite/index.php/subaction
or
http://www.mywebsite/index.php/mymodule/subaction
Note :: I do not want to create new controller I want this in the same index controller.
Magento URL-to-controller matching works through the Standard router which expects URLs to have a specific form. If you want to change that you have a few options at your disposal:
Deprecated config-based URL rewrites
Create URL rewrite entries in core_url_rewrite table
Create a custom router class to match the URL patterns which you would like to use
When considering how URL matching should work, you need to consider how Magento will expect to build the URL using its native URL calculation tools as well as how to get requests to match.
You can do this by using routes
in your bootstrap do the following
protected function _initMyRoutes() {
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$router = $front->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', APPLICATION_ENV);
$router->addDefaultRoutes();
$router->addConfig($config, 'routes');
return $router;
}
and in the configs directory create a file called routes.ini and in it place the following
routes.myRoute.type = "Zend_Controller_Router_Route_Static"
routes.myRoute.route = "/subaction/"
routes.myRoute.defaults.module = "mymodule"
routes.myRoute.defaults.controller = "index"
routes.myRoute.defaults.action = "subaction"
OR
you can add the route directly in your bootstrap with
protected function _initMyRoutes() {
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$router = $front->getRouter();
$router->addDefaultRoutes();
$route = new Zend_Controller_Router_Route_Static(
'subaction',
array('module' => 'mymodule', 'controller' => 'index', 'action' => 'subaction')
);
$router->addRoute('subaction', $route);
return $router;
}
That should do the trick but be adviced using routes can really be a pain.
More about routes in the ZF manual

Resources