How to convert _ to - in urls for codeigniter? - codeigniter

Been trying to learn codeigniter, 1 problem I had though is that if I had a function say
top_10()
It would mean that my urls will be something like
..../top_10/
Which is fine but I prefer - more than _ for urls. I tried changing my function names to top-10(), but it results in a syntax error it seems (even if it doesn't it results in a ugly function name), Is there a way to let codeigniter auto converts all the _ in my controller functions to - when it comes to the urls?
.../top_10/ -> .../top-10/ for all other similar controller functions.

You can use the URI routing rule
something like :
$route['controller/top-10'] = "controller/top_10";
Add this line in the routes.php file inside application/config/routes.php
Here controller/top-10 will be the part of your url which will be routed to the desired function provided by on the right hand side.
also if you aren't using mod_rewrite to rewrite your url then your URL will appear some thing like
http://localhost/index.php/controller/top-10

Inside your application/config/routes.php folder if there isn't a line for translating uri dashes then add this line: $route['translate_uri_dashes'] = TRUE;
If $route['translate_uri_dashes'] already exists, simply set it's value to true.

Related

CodeIgniter displays page with no css instead of 404

I am new to CodeIgniter and I have some issues with its routing config.
Everything works fine if I access a page as it should be
http://localhost/index.php/class/controller
But if I add some random arguments
http://localhost/index.php/class/controller/randomStuff
it displays the page without the CSS or the JS but I would like to display a 404 error page.
Here is my route.php file
$route['stats'] = 'main/account';
$route['/'] = 'index.php';
$route['/(:any)'] = 'main/disperr'; //Tried this to solve the problem but doesn't work (disperr simply returns show_404())
$route['default_controller'] = 'main';
$route['all'] = 'main';
$route['404_override'] = 'errors/page_missing';
$route['translate_uri_dashes'] = FALSE;
Thanks in advance for your help.
Edit : Thank you for your answers !
After routing the pages correctly I stil had the missing css issue.
As I'm using twig, I sent the base_url() as a parameter to my view and added it to the path to my CSS and JS.
It works now.
you may plese the following url
http://localhost/class/index.php/main
after index.php you need to give your name of the controller(not folder name controller)
You have to follow some rules from documentation page.
Routes you found after CodeIgniter installation have to be at the beginning in order you found those.
$route['default_controller'] = 'main';//Main class need to have index() method for this works
$route['404_override'] = 'errors/page_missing';
$route['translate_uri_dashes'] = FALSE;
You have to take care of precedence because rules will be read in order those are defined in file.
$route['all'] = 'main';
$route['stats'] = 'main/account';
$route['(:any)'] = 'main/disperr'; //Tried this to solve the problem but doesn't work (disperr simply returns show_404())
If you have (:any) wildcard placeholder it has to be at the end of file because you have to check all specific routes not to pass (:any) rule, because that rule is valid for any route (as name suggest). Slash to index route is not needed because slash is default controller and it is already defined. Routing to index.php is not valid and you are suppose to route just to 'controller_name/method_name/param1' or similar pattern. No need for slashes at the beginning, as you see I removed those. Maybe removing slashes from the beginning of made routes will solve your CSS issue, but you need to follow other documentation rules I exposed here. Read full page here.

CodeIgniter - How to route a controller with other name?

I have installed an authentication module named bit_auth in my codeIgniter.
I have a controller for that module named "bit_auth" so when calling functions on my controller my urls will be like this:
http://(mydomain.com)/bit_auth/
http://(mydomain.com)/bit_auth/edit_user/1
http://(mydomain.com)/bit_auth/activate/7b60a33408a9611d78ade9b3fba6efd4fa9eb0a9
Now I want to route my bit_auth controller to be called from http://(mydomain.com)/auth/....
I have defined these routes in my "config/routes.php":
$route['auth/(:any)'] = "bit_auth/$1";
$route['auth'] = "bit_auth";
It's working fine when I use: http://(mydomain.com)/auth/
but it shows me 404 page not found error when opening links such as:
http://(mydomain.com)/auth/edit_user/1
http://(mydomain.com)/auth/activate/7b60a33408a9611d78ade9b3fba6efd4fa9eb0a9
What am I doing wrong?
Because you are using more params than are in the route you will have to do this:
$route['auth/(:any)/(:num)'] = "bit_auth/$1/$2";
Hope this helps!
After googling and investigating I saw somewhere they where using another syntax for routing in CodeIgniter which was using regular expression directly rather than using codeigniter described patterns like (:any) or (:num) in its help. I just replaced (:any) with (.+) in my config/routes.php and now it is working perfect.
$route['auth/(.+)'] = "bit_auth/$1";
$route['auth'] = "bit_auth";
Because in CodeIgniter (:any) and (:num) are not including / and they are aliases for regular expression patterns of ([^\/]+) and (\d+) so if you want to match rest of the link including any number of / you can use manual regular expression pattern (.+) which includes / in its pattern and will trigger for the all rest of URL.

How can I shorten routes in Codeigniter for certain requests?

I have a page that has this category URL website.com/category/view/honda-red-car and I just want it to say http://website.com/honda-red-car no html or php and get rid of the category view in the URL.. this website has been done using the CodeIgniter framework..
also this product view URL website.com/product/details/13/honda-accord-red-car
and I want it to be website.com/honda-accord-red-car PLEASE HELP!!!
I cannot find correct instructions on what I am doing wrong??
In Routes.php you need to create one like so
$route['mycar'] = "controller_name/function_name";
So for your example it would be:
$route['honda-red-car] = "category/view/honda-red-car";
Take a look into the URI Routing part of the user guide.
If you have concrete set of urls that you want to route then by adding rules to the application/config/routes.php you should be able to achieve what you want.
If you want some general solution (any uri segment can be a product/details page) then you might need to add every other url explicitly to the routes.php config file and set up a catch-all rule to route everything else to the right controller/method. Remember to handle 404 urls too!
Examples:
Lets say the /honda-red-car is something special and you want only this one to be redirected internally you write:
$routes['honda-red-car'] = 'product/details/13/honda-accord-red-car';
If you want to generalize everything that starts with the honda- string you do:
$routes['(honda-.*)'] = 'product/details_by_slug/$1'; // imaginary endpoint
These rules are used inside a preg_replace() call passing in the key as the pattern, and the value as the replace string, so the () are for capture groups, $1 for placing the capture part.
Be careful with the patterns, if they are too general they might catch every request coming in, so:
$routes['(.*)'] = 'product/details_by_slug/$1';
While it would certainly work for any car name like suzuki-swift-car too it would catch the ordinary root url, or the product/details/42 request too.
These rules are evaulated top to bottom, so start with specific rules at the top and leave general rules at the end of the file.

Remove controller name from codeigniter 2 url path

I am having trouble removing the controller name from my url path on my localhost.
i have this url - localhost:8888/localhost/site_name/
i have been able to remove index.php from the url using my htaccess similar to http://codeigniter.com/wiki/mod_rewrite so that:
localhost:8888/localhost/site_name/index.php/controller_name
is now:
localhost:8888/localhost/site_name/controller_name/
but i can't remove the controller name from the path so that:
localhost:8888/localhost/site_name/controller_name/function_name/
becomes:
localhost:8888/localhost/site_name/function_name/
I am using only one controller, and i have added:
$route['^(function_name1|function_name2|function_name3)(/:any)?$'] = 'controller_name/$0';
$route['^(?!ezstore|ezsell|login).*'] = "home/$0"; /*similar variation i tried*/
and other variations to my routes file but it does not have any effect. i also tried using the _remap function but that does not help in this case.
Any help will be appreciated! Thanks
You can use a wildcard route,
$route['(:any)'] = "controller_name/$1";
Then when if you go to http://localhost/function_one/param1
it will call the controller controller_name the function function_once and pass param1 as the first parameter.
nb: I must point out, using only one controller for an entire site does raise warning bells for me, you may want to get your code design checked out, but that's just me.

Change CodeIgniter route for static pages and auth login [duplicate]

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

Resources