How to set up routing in codeigniter? - php

This is what I've in the config/routes.php
$route['music/artist/:any'] = "music/artist/index/$1";
And I've my controller, 'artist', inside a folder music/
I tried to get the uri segment inside the index method from the artist controller but when I go to the browser with domain.com/music/artist/abc it returns page not found.
How can I make this work? But I want to have my controller inside that folder.
Any help will be great. Thanks.

Use this in config/routes.php
$route['music/artist/(:any)/(:any)'] = "music/artist/index/$1";

Related

access controller inside the controller directory within another folder

This is my controller directery structure
controllers
-----user(folder)
----------User_reg.php //My Controller inside user folder.
-----index
-----Welcome.php
I can access User_reg.php like this:
http://localhost/coin_system/user/user_reg
Now I want to eliminate user from this URL so I added the route
$route['user_reg/(:any)'] = 'user/user_reg/$1';
But it show the error: 404 Page Not Found
I want to access it like this
http://localhost/coin_system/user_reg
How can i access the controller inside the directory in controller?
I have tried to solve using this SO question but it did't help.
I am using Codeigniter latest version. 3.1.5
You have missed function
https://www.codeigniter.com/user_guide/general/routing.html#examples
$route['user_reg'] = 'user/user_reg/index';
$route['user_reg/(:any)'] = 'user/user_reg/index/$1';
Or You can have different function
$route['user_reg'] = 'user/user_reg/somefunction'
$route['user_reg/(:any)'] = 'user/user_reg/somefunction/$1';
Also try with index.php in url
http://localhost/coin_system/index.php/user_reg
When you add route with :any it also find your calling method after controller. But for index(which is default) its not compulsory to all index method So you need to specify route for it also. So just need to add one more route
$route['user_reg'] = 'user/user_reg'; // add this to route file
$route['user_reg/(:any)'] = 'user/user_reg/$1';

Routing with codeigniter with customized URL

I have a controller named home, which is my default controller too.
This is my path
http://192.168.1.100/FMP/mobile/home/index/suzuki-violin-school--6/102271
Now i am trying to rewrite the url as
http://192.168.1.100/FMP/mobile/suzuki-violin-school--6/102271
Where i need to remove both controller name and function name.
So far i tried putting this in route.php
$route['(?!user|product).*'] = "mobile/home/index/";
But it takes all other methods in home controller and other controllers to the index function of home controller.
Any thoughts on how can i achieve this?
Try this below code in end of your routes.php file
$route['(:any)/(:num)'] = 'home/index/$1/$2';
above route get two parameter and pass it into home page index function and your index function will be ass follow
public function index($parms1,$parms2)
Hope it will solve your problem. If not let me know. I'll happy to guide you through

CodeIgniter controllers conflict with sub folder

I wrote a controller named Admin with method index and placed as /application/controllers/admin.php for URL http://localhost/admin.
Then I wrote another controller named Account inside /application/controllers/admin/ folder, with a method login for URL http://localhost/admin/account/login.
But the problem is when I visit /admin/account/login I got a 404. I don't know why? Or how can I define this 2 path in my controllers?
I also faced the same problem but for only solution was to either rename the subfolder name or rename the outside controller name.
Try put admin/account/login before admin, something like this:
$route ['admin/account/login'] = 'admin/account/login';
$route ['admin'] = 'admin/index';

Remove or hide controller name from url in codeigniter

How can I remove controller name from url . I have two controller
home and admin
and the url's are
http://domain.com/likes/home/post/sports/20-Athletes-Who-Profited-While-in-College-/12
home/post
and
http://domain.com/likes/admin/ad_managment/edit/2
http://domain.com/likes/admin/meta_tags_home/edit/2
admin/ad_managment admin/meta_tags_home
I have already used this
$route['(:any)/(:any)/(:num)'] = "home/post/$1";
It works for this URL
http://domain.com/likes/home/post/sports/20-Athletes-Who-Profited-While-in-College-/12
admin is not working. Basically I want to remove home/post Leave admin controller it doesn't matter
try this
$route['(:any)/(:any)/(:any)'] = "home/post/$1";
Try this example
$config['base_url'] = 'http://your-site.com/';
And in your routes.php
$route['default_controller'] = 'main';
Through this method you can remove controller name from url.
check user guide of codeigniter and in this example see how the routing done.
Read more about code-igniter routing

Codeigniter routing question

I have a CI application with organized folders.
I have several folders on views, controllers and models.
ie.:
/controllers/frontend/, controllers/backend
/views/frontend, views/backend
... etc ...
So if i want to access the function 'login' on the frontend controller i have to go to: http://localhost/frontend/login/index
What i want is to get rid of the need of typing 'frontend', so if i type http://localhost/login/index, it would be the same as http://localhost/frontend/login/index.
Of course i dont want to add them manually to the routes file, i want it to be recognized automatically.
Thanks.
Try changing line 17 in your config.php file in application\config.
$config['base_url'] = 'http://localhost/';
Change above to below:
$config['base_url'] = 'http://localhost/frontend/';
I know you don't want to change your routing for each controller, but if this is the only one you need to do a specific routing for, you can change the default controller in the Router file inside your application/config folder to:
$route['default_controller'] = 'frontend';
$route['(:any)'] = "frontend/some_secondary_variable";
The second line is only if you need to pass variables to the controller, otherwise, omit it.

Categories