CodeIgniter - How to route a controller with other name? - codeigniter

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.

Related

How to pass file path with '/' to route laravel?

How can we pass file path to route like this,
<a href={{route('route.name',['path'=>'uploads/xyx/'.$id.'/'.$attachment_name])}}>download</a>
However, I would like this to hit
Route::get('download/{path},'Controller')->name('route.name')
When I hit this route my url got transformed somehow like this -> download/uploads/44/filename which is causing not found exception!
I want to pass the path as a parameter, where slashes should have to be ignored! so that I can get full path in my controller!
As per the documentation you can do this:
"The Laravel routing component allows all characters except /. You must explicitly allow / to be part of your placeholder using a where condition regular expression"
Route::get('download/{path}', ...)->where('path', '.*');
Laravel 7.x Docs - Routing - Parameters - Encoding Forward Slashes
use get parameter remove {path}
Route::get('download,'Controller')->name('route.name')
in blade
<a href={{route('route.name',['path'=>'uploads/xyx/'.$id.'/'.$attachment_name])}}>download</a>
this will generate url like route.name?path=download/uploads/44/filename
still u will get data in controller like $request->path

Shorten URLs within CodeIgniter

This question has been asked a few times but I can't seem to find a solution that helps me which is why I am trying here.
I have my site setup with the following for URLs I am using CodeIgniter I have a controller called user which loads a user view.
So my URLs are structured as follows:
http://example.com/user/#/username
I want to try and strip out the user controller from the URL to tidy up my URL so they would just read:
http://example.com/#/username
Is this possible I have been looking at route and have tried lots of different options but none have worked?
$route['/'] = "user";
Could anyone offer any solution?
Assuming the '#' in your URLs is a valid function and 'username' is a parameter for that function, then this route should work:
$route['#/(:any)'] = "user/#/$1";
Depending on what usernames are to be routed you may want to change the wildcard. For example, if you only wanted to route numbers as the parameter, you could change (:any) to (:num).
(:num) will match a segment containing only numbers.
(:any) will match a segment containing any character.
You can also use regular expressions to define routing rules, allowing you to further restrict what is routed.

Codeigniter URL routing solution for my website

I have a website that is developed with CodeIgniter. I have added the route for my url as follows:
$route['about_us'] = 'about-us';
Now I have a problem with that. I.e. when I am looking for the url www.mysite.com/about_us it works and at same time www.mysite.com/about-us is also working. I want only one url to work: the one with the underscore.
I have removed this to:
$route['about_us'] = 'about-us';
But the url www.mysite.com/about-us still works. It may cause duplicate content for my website in Google and so more page links also showing. Even I don't have that functions too. Like www.mysite.com/about_us/design. Likewise in about_us controller file index function only there, but design method calling in Google.
How do I resolve this problem?
You actually don't need a route here. The normal purpose of request routing the way you are using it is so that you can use hyphenated URLs when hyphens are not permitted in class and function names. I.E. you want the url to by www.example.com/test-controller, but you can't actually name a controller test-controller because the hyphen is illegal.
If you only want to have the underscored URL such as www.mysite.com/about_us then just remove the route completely and name the controller about_us. With no routing rules the hyphenated url should 404.

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.

How to convert _ to - in urls for 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.

Resources