How to use this things, the routes ?
For example, i my page is
controller/function
and i want the controller part and the function part to become
name
$route['controller/function'] =
"name";
and i tryed like that, but nothing happens, still when trying to go to controller/function it is controller/function not name
you need to do it the other way round.
actually you define a route to a desired controller function:
$route['en/name'] = 'en_controller/name_function';
also see here: CI Routing
You should read the manual URI Routing in CI URI Routing. There are some examples that will help you understand how to make your routing to work.
Related
I want to change Codeigniter router
ex. cms_aboutus_cate
to
controller
--cms
--aboutus
--cate.php
is there any other efficient way to achieve using routing or any other way?
$route['^cms_aboutus_cate$'] = "cms/aboutus/cate/index";
Is important that you set the index method.
I recommended you HMVC if you want modulate your app.
You can set your url in config/routes.php file.
In your case, you can put:
$route['cms_aboutus_cate'] = 'cms/aboutus/cate';
So, the actual URL will be http://yourdomainname/cms/aboutus/cate but it will show in URL is this: http://yourdomainname/cms_aboutus_cate.
Reference:
https://www.codeigniter.com/userguide3/general/routing.html
I have searched everywhere but i can't find appropriate result. I want to remove controller_name from every url of the site. My codeigniter is installed in subfolder of domain.
For Example:
www.site_name.com/subfolder/controller_name/any_method_name
I have only one controller from which i am calling all the methods.
I have learned that i have to do some changes in routes but i think that is only for one url. so, how to remove for every url.
Not sure if this works but you could try adding this in your application\config\routes.php:
$route['([a-z]+)'] = 'controller_name/$1';
The documentation https://www.codeigniter.com/userguide3/general/routing.html#regular-expressions
Here, I am answering my own question. I have solved this problem by following solution.
Suppose, my controller name is users, then the answer is:
$route['^(:any)(/:any)?$'] = "users/$0";
This removes controller name from every url of the website.
Codeigniter support two types of routing rules
1)Wildcards
2)Regular Expressions
I prefer Wildcards
in routes just place this one
$route['login/(:any)'] = "v1/login";
A URL with "login" as the first segment, and anything in the second will be remapped to the "v1" class and the "login" method.
means your change www.abc.com/login instead of www.abc.com/v1/login
check it once routing in codeigniter here https://www.codeigniter.com/userguide3/general/routing.html .......
I've this in my config/routes.php:
$route['music/artist/(:any)'] = "music/artist/index/$1";
I've an artist controller inside controller/music/ and index method.
So if I go to browser with domain.com/music/artist/michael then with the following line in controller I can get the data michael.
$this->uri->segment(3);
Please correct me if I'm wrong but here's the real question.
Now I want to have this on link.
domain.com/music/artist/michael/video/beat-it/
I want to have a video function inside the artist controller and inside I want to get the dynamic data, michael and beat-it.
So the routes config I've these:
$route['music/artist/(:any)'] = "music/artist/index/$1";
$route['music/artist/(:any)/video/(:any)'] = "music/artist/video/$1/$2";
If I go to the video link it seems it just hit the index function.
How can I make the video link work?
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
Switch the places of routes. Most specific routes need to be parsed first because (:any) placeholder would take anything there.
$route['music/artist/(:any)/video/(:any)'] = "music/artist/video/$1/$2";
$route['music/artist/(:any)'] = "music/artist/index/$1";
Reference.
Just got a question about codeigniters routing when you only want the first segment to be valid. Cant seem to find a good answer when googling.
So I have a basic route for my general pages:
$route['(:any)'] = 'common/pages/view/$1';
Pages is the class and view is the method along with the name of the page as a variable (pretty much like the example on the ci manual).
This works fine when I go to:
www.mysite.com/mypage/
However when I then go to:
www.mysite.com/mypage/randomstring/
This also loads mypage which is essentially a duplicate.
Is their a way to tell the any route to only apply to the first segment and if more exist do a 404?
If worse comes to worse I will just add a check in the method to see if the 2nd segment exists, if so show_404 but just curious as to if it can be done purely in the routes.
Thanks for reading and I hope that makes sense.
You could just use Regex instead?
$route['([^/]+)'] = 'common/pages/view/$1';
This would prevent the URL after your domain from containing / and if it does, it will call the default 404 page.
I haven't tested this but it "should" work ;)
Try this:
$route['(:any)/(:any)'] = "none_existent_controller";
$route['(:any)'] = "common/pages/view/$1";
I need to create a dynamic url in codeigniter like the facebook application. Is it possible to create such url using the codeigniter framework?
eg:
1. www.facebook.com/nisha
2. www.facebook.com/dev
You need to set up custom routing for the controller in application/config/routes.php. Like:
$route['([a-zA-Z]+)'] = "controller_name/function/$1";
This makes urls like the way you want, but it makes all of your controller inaccessible, that is because any '/controllername/parameter/' format will match with '(:any)' and will be redirected to our 'controller_name/function/'.
To stop controllers redirected by the CI router, you will have to explicitly define all of your controllers on the routes.php first then add the above mentioned routing rule at last line. Thats how i made it to work.
Hope that helps you in some way.
Its pretty easy to setup this by the use of routes. Read their routing guide
$route['([a-zA-Z]+)'] = "controller/user/$1";
However, if their is only one way of accessing the website, is like domain.com/username then its ok, otherwise, this will prove be a hard catch on the long run. On that case, limit the Route to a limited scope like
$route['users/([a-zA-Z]+)'] = "controller/user/$1";
This will help in the extending the system in numerous way
Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.
//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {
$route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}