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";
Related
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 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";
}
I have a problem with Codeigniter routes. I would like to all registered users on my site gets its own "directory", for example: www.example.com/username1, www.example.com/username2. This "directory" should map to the controller "polica", method "ogled", parameter "username1".
If I do like this, then each controller is mapped to this route: "polica/ogled/parameter". It's not OK:
$route["(:any)"] = "polica/ogled/$1";
This works, but I have always manually entered info in routes.php:
$route["username1"] = "polica/ogled/username1";
How do I do so that this will be automated?
UPDATE:
For example, I have controller with name ads. For example, if you go to www.example.com/ads/
there will be listed ads. If you are go to www.example.com/username1 there are listed ads by user username1. There is also controller user, profile, latest,...
My Current routes.php:
$route['oglasi'] = 'oglasi';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';
I solved problem with this code:
$route['oglasi/(:any)'] = 'oglasi/$1';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';
Regards, Mario
The problem with your route is that by using :any you match, actually...ANY route, so you're pretty much stuck there.
I think you might have two solutions:
1)You can selectively re-route all your sites controller individually, like:
$route['aboutus'] = "aboutus";
$route['where-we-are'] = "whereweare";
//And do this for all your site's controllers
//Finally:
$route['(:any)'] = "polica/ogled/$1";
All these routes must come BEFORE the ANY, since they are read in the order they are presented, and if you place the :any at the beginning it will happily skip all the rest.
EDIT after comment:
What I mean is, if you're going to match against ANY segment, this means that you cannot use any controller at all (which is, by default, the first URI segment), since the router will always re-route you using your defined law.
In order to allow CI to execute other controllers (whatever they are, I just used some common web pages, but can be literally everything), you need to allow them by excluding them from the re-routing. And you can achieve this by placing them before your ANY rule, so that everytime CI passed through your routing rules it parses first the one you "escaped", and ONLY if they don't match anything it found on the URL, it passes on to the :ANY rule.
I know that this is a code verbosity nonetheless, but they'll surely be less than 6K as you said.
Since I don't know the actual structure of your URLs and of your web application, it's the only solution that comes to my mind. If you provide further information, such as how are shaped the regular urls of your app, then I can update my answer
/end edit
This is not much a pratical solution, because it will require a lot of code, but if you want a design like that it's the only way that comes to my mind.
Also, consider you can use regexes as the $route index, but I don't think it can work here, as your usernames are unlikely matchable in this fashion, but I just wanted to point out the possibility.
OR
2) You can change your design pattern slightly, and assign another route to usernames, something along the line of
$route['user/(:any)'] = "polica/ogled/$1";
This will generate quite pretty (and semantic) URLs nonetheless, and will avoid all the hassle of escaping the other routes.
view this:
http://www.web-and-development.com/codeigniter-minimize-url-and-remove-index-php/
which includes remove index.php/remove 1st url segment/remove 2st url sigment/routing automatically.it will very helpful for you.
I was struggling with this same problem very recently. I created something that worked for me this way:
Define a "redirect" controller with a remap method. This will allow you to gather the requests sent to the contoller with any proceeding variable string into one function. So if a request is made to http://yoursite/jeff/ or http://yoursite/jamie it won't hit those methods but instead hit http://yoursite/ remap function. (even if those methods/names don't exist and even if you have an index function, it supersedes it). In the _Remap method you could define a conditional switch which then works with the rest of your code re-directing the user any way you want.
You should then define this re-direct controller as the default one and set up your routes like so:
$route['(.*)'] = "redirect/index/$1";
$route['default_controller'] = "redirect";
This is at first a bit of a problem because this will basically force everything to be re-directed to this controller no matter what and ultimately through this _remap switch.
But what you could do is define the rules/routes that you don't want to abide to this condition above those route statements.
i.e
$route['myroute'] = "myroute";
$route['(.*)'] = "redirect/index/$1";
$route['default_controller'] = "redirect";
I found that this produces a nice system where I can have as many variable users as are defined where I'm able to redirect them easily based on what they stand for through one controller.
Another way would be declaring an array with your intenal controllers and redirect everything else to the user controller like this in your routes.php file from codeigniter:
$controllers=array('admin', 'user', 'blog', 'api');
if(array_search($this->uri->segment(1), $controllers)){
$route['.*'] = "polica/ogled/$1";
}
How can I rewrite the following codeigniter url
localhost/test_project/pages/show_page/11
to
localhost/test_project/pages/11
and is it possible to further reduce like:
localhost/test_project/11
Thanks.
You should edit this file: application/config/routes.php.
Let's assume you have a pages controller and a show_page method with receives an id as a parameter (just as you said).
your URL is like localhost/test_project/pages/show_page/11
I assume that the parameter is always a number, so using wildcards as described here you can add the following to your routes.php file:
$route['pages/(:num)'] = "pages/show_page/$1";
So your URLs will be like localhost/test_project/pages/3. This is like saying if anyone tried to visit localhost/test_project/pages/3, he/she actually means localhost/test_project/pages/show_page/3.
For urls like localhost/test_project/3 you can add this:
$route['(:num)'] = "pages/show_page/$1";
You've a few options. These are in descending order of priority.
First there is routes.php, which is the generally preferred way of handling things (information already provided, but here it is again).
You can also add a _resolve method to a controller (which means that CI needs to get to your controller to begin with), and
finally you can override the CI_Router library, the class which actually returns the route to go to.
Seriously, don't override CI_Router unless you know what you're doing.
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.