CodeIgniter controllers conflict with sub folder - php

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';

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

How to set up routing in codeigniter?

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

Codeigniter: Index page creation

My folder structure is as following
admin
__master
_______address_book
_______Users
_______Product
_______etc
__operations
_______register_orders
_______payments
_______etc
I have created controllers for address_book,users,products,register_orders, payments etc to reduce the complexity of each controller.
Now, how to handle index page request for www.abc.com/admin ?
I have created Admin controller in /controllers directory then other links like
www.abc.com/admin/master/address_book will not work.
How to handle both requests?
I would also like to know is there any way to handle each index page requests
eg:
www.abc.com/admin/
www.abc.com/admin/master/
www.abc.com/admin/operations/
To access each request with url like-
www.abc.com/admin/
www.abc.com/admin/master/
www.abc.com/admin/operations/
you have to use codeigniter's routing. And for routing there are a config file under aplication/config folder named routes.php. Add all your routes in this file. Suppose you want to access this url-
www.abc.com/admin/operations/
then you have to create a new route for this in the route file, like-
$route['admin/operations'] = 'admin/operations';
where in $route array index you have to mention what will be the url and the value against this index you have to mention the controller's path and also you can mention the controller's function name which will be invoked (for index function there no need for mentioning).

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