I have pages that are generated from a database, based on the URI. Functons as it should, however I can't set-up my routes to eliminate the controller and function from the URL.
$route['studios/(:any)'] = 'studios/location/$1';
Right now, I have the route to show the controller name and the URI variable (whatever that may be). However, I want to eliminate the controller name as well and just display the URI variable that's called as the URL. Hard to explain - hopefully someone picks up my drift...
Current URL would be: domain.com/studios/studio1
But I want to just display: domain.com/studio1
I tried $route['/(:any)'] = 'studios/location/$1';, but that's messing up my entire site.
Help?
$route['studios(/:any)*'] = 'studios/location';
This route will force everything from studios on to studios/location. You can then access any of the parameters using URI segments:
$id = $this->uri->segment(2);
If your URL was somewhere.com/studios/location/2, $id would resolve to 2
However, since you want it to just be from the root on, you will have to put your override route at the bottom of the routes file so it is assessed last:
// all other routes here. Which must be specifically
// defined if you want a catch all like the one you mentioned
$route['(:any)'] = 'studios/location';
Alternatively, if you want a high maintenance site, you can specify a collection of routes like so:
$route['(studio1|studio2|studio3)'] = 'studios/location/$1';
how is it "messing up your site"?
In any case, you should not have the / before (:any)
Just:
$route['(:any)'] = 'studios/location/$1';
EDIT:
BEFORE the $route['(:any)'], you'll need to specify routes fro all your controllers; this is pretty normal, don't know if I'd call it "high maintenance", but you'll need to decide
Related
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.
I have two different controllers which I want to route to the same URL.
For example,
$route['dashboard/(:any)'] = 'admin/crud/$1';
$route['dashboard/(:any)'] = 'admin/dashboard/$1';
But this is causing 404 errors.
I guess there is some issue with the :any wildcard.
Is there an alternative to use?
CodeIgniter doesn't map controllers to URLs, it maps URLs to controllers. See URI Routing.
You are trying to map two same exact URLs to go to different places. This doesn't make sense.
Also, since $route is just an associative array, you are overwriting the value instead of adding an additional route.
$route['dashboard/(:any)'] = 'admin/crud/$1';
$route['dashboard/(:any)'] = 'admin/dashboard/$1'; //Immediately over writes the previous value
So, it looks like you just have a problem with the second route:
$route['dashboard/(:any)'] = 'admin/dashboard/$1';
Since, admin is the folder, double check that the value being passed in by the route actually is a method in your dashboard controller class.
Also, check out this question and accepted answer: routing controllers in sub folders - codeigniter I think it provides an example of what you are attempting to do.
I have controller called 'project' with method of index
I want to pass project_id
it works with this url:
URL: http://example.com/project/index/project_id
what I am trying to do is to have url for each project
http://example.com/project_id
for example:
http://example.com/rickyboby
I tried this code
$route['(:any)'] = "project/index/$1";
It worked I could access project_id from url www.example.com/project_id but the problem is I have other controllers are not working now, for example I have welcome controller, the url: www.example.com/welcome/ is routing me to project page, how can I solve that ?
You need to whitelist all the controllers you have before the "any" rule because they go in order (so in your example, /welcome would take you to project/index/welcome). For example:
$route['welcome'] = "welcome";
$route['somethingElse'] = "someOtherUrl";
$route['(:any)'] = "project/index/$1";
You can also try to think of a clever way of doing this with regex, but I think you're better off with a whitelist.
This is a basic programming concept and probably doesn't belong here, but you can always do something like:
$controllers = array('welcome', 'otherController', 'etc');
foreach($controllers as $c) {
$route[$c] = $c; // make all URLs from the list go to their same-named controllers
}
$route['(:any)'] = "project/index/$1"; // handle the other URLs
One more thing - don't forget to prevent users from giving projects names that match existing controller names (such as welcome) because their project won't be accessible! There, yet another reason to have that controller list someplace...
Place the "welcome" routing rule before the "project" routing rule. This way the system will match welcome and won't go looking for the next rule. Alternatively, you could use (:num) instead of (:any), this way it will match only numbers (in case your project ids are numbers).
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";
}
With CodeIgniter I'm trying to create a URL structure that uses a title string as the entire URI; so for example: www.example.com/this-is-a-title-string
I'm pretty confident I need to use the url_title() function in the URL Helper along with the routes.php config folder but I'm stuck bringing it all together.
Where do I define the URI and how is it caught by the routes folder?
Seems to be a straight forward problem but I'm getting stuck creating the URLs end-to-end. What am I missing?
I thought about a catch-all in the routes folder: $route['(.*)'] = "welcome/controller/$1"; ....but how would this work with multiple functions inside a particular controller? ...and maybe it's not even the right way to solve.
You can send all requests to a driver with something like this:
$route['(:any)'] = "welcome/function";
Then use the _remap function to route requests inside the controller.
However, using URL's as you suggest limits the CI functionality. Try something better like www.example.com/article/this-is-a-title-string
$route['article/(:any)'] = "articles/index";
and in article (controller), use _remap...
If you're going to re-route every request, you should extend CI_Router.
The actual implementation depends on what you're doing. If you customize CI_Router, you can do it AFTER the code that checks routes.php, so that you can keep routes.php available for future customization.
If the URI contains the controller, function, and parameters, you can parse it within your extended CI_Router and then continue with the request like normal.
If the URI is arbitrary, then you'll need something (file, db, etc) that maps the URI to the correct controller/function/parameters. Using blog posts as an example, you can search for the URI (aka post-slug in WordPress) in the db and grab the corresponding record. Then forward the request to something like "articles/view/ID".