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.
Related
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';
Can I use the same routes but access different controllers? I want my url like this
example.com/article-name/
example.com/category-name/
My routes are:
$route['([^/]+)/?'] = 'Article/index/$1';
$route['([^/]+)/?'] = 'Category/index/$1';
I cannot find out how to do this.
Change the .htaccess file.
Then, in config.php change line no:38 as given below
$config['index_page'] = '';
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';
My current url is universitytwig.com/user/userprofile/cm.cm , where user is the controller , userprofile is the method and cm.cm is the parameter .How i change this url to universitytwig.com/cm.cm by route.php or by routing from database, Or by .htaccess
Try to add
$route['cm.cm'] = 'user/userprofile/cm.cm';
to your application/config/routes.php
Or
$route['(:any)'] = 'user/userprofile/$1';
if your parameter can be different.
you can use the routes.php but if you make a rule in something like cm.cm to user/userprofile/cm.cm
$route['(:any)']= 'user/userprofile/$1';
But this is not a perfect solution it will point all the controllers of every thing after base url to user/userprofile/ you need to specify some string the CI routes will identify that this needs to be point on this controller if cm.cm is dynamic parameters not hard coded for this your url structure should be
universitytwig.com/students/cm.cm
and in routes you can now make a rule
$route['students/(:any)']= 'user/userprofile/$1';
This will work for your scenario
I am wondering if there is any other configuration options for a default controller.
For example - if I have a controller called "site" and I set the default controller in the following file: application/config/routes.php to:
$route['default_controller'] = "site";
I should be able to go to http://localhost and that brings up the
index(); function in the site controller.
However, if I try to do go to http://localhost/index.php/index2 to load the index2(); function I get a 404 error. If I change the URL to http://localhost/index.php/site/index2 it works fine - but I thought already set the default controller. Is there any way around this?
The only way to do it is manually writing the routing rule:
$route['index2'] = "site/index2";
You cannot do that.
The default_controller is only for URLs without any URI parameter. How could CodeIgniter distinguish between a method name and a controller name?
What you can do:
define a redirect inside your 404 document or directly map your 404 document to index.php
No CI doesn't work like that the first parameter has to be the name of a controller, so in this case you would have to create a controlled called "index2".