URL routing in PHP Codeigniter - php

I am having entry in my route.php like - $route['admin/students'] = 'view_student'. Here view_student is controller name. Now when from "localhost/school/admin" page I call Students, than everything works fine; But when I change my route like - $route['/school/admin/students'] = 'view_student', and call it from "localhost/school/admin" page as Students, than 404 page is shown. Whats wrong in here?

Try this code it might help you :
Here dashboard is the name of controller
//this will route as localhost/appFolder/admin/index
$route['admin'] = 'dashboard'; // for your index page
//this will route as localhost/appFolder/admin/method_name
$route['admin/(:any)'] = 'dashboard/$1';
//this will route as localhost/appFolder/admin/method_name/param1
$route['admin/(:any)/(:any)'] = 'dashboard/$1/$2';
Link the route Like
// for your index page
// for your other pages
To link the other controller defined just like

school is your ci root, so if you define $route['/school/admin/students'], it will seek school class with admin function, that never exist, instead of admin route.
you should read the documentations first before make any step,
https://www.codeigniter.com/userguide3/general/routing.html

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

codeigniter form post from a folder inside controller not working

So, here is my problem.
In codeigniter, I have a folder inside controllers named admin and the controller admin.php inside that.
The problem is when i try posting a form from this controller to a controller outside admin folder, it does'nt works. I m really getting frustated trying to figure out where exactly i am doing wrong.
my routes.php is-
$route['admin/(:any)'] = "admin/$1";
$route['(:any)'] = "welcome/$1";
$route['default_controller'] = "welcome";
$route['404_override'] = '';
and my base url is $config['base_url'] = 'http://localhost/ci_extend/';
Actually the form is getting posted to self.
I know, surely there is routing problem but just cant figure it out. Can someone help me out?
Since you have your controller in your admin folder named admin.php
you should route it as below
$route['admin/(:any)'] = "admin/admin/index/$1";
I've included function name i.e. index , I guess the function name is required, give a hit without function name if you want to, but I don't see any problem in keeping it.
Accepted Answer
Change $route['(:any)'] = "welcome/$1"; > $route['welcome/(:any)'] = "welcome/$1"; & if using <form> make sure CSRF either disabled or you included CSRF token in the form

How to pass value/variable directly to default controller in codeigniter?

Assume base_url is "http://www.facebook.com"
Default controller is "Home"
How do i code my routes and controller to get my URL as "http://www.facebook.com/noddycha"
Where "noddycha" is a get parameter. The page should load my profile page from DB.
In your application/routes.php file add this line after $route['404_override'] = '';:
$route['(:any)'] = 'users/profile';
Now absolutely everything will be redirected the profile function of the users controller (or whichever controller/function combination you substituted in the routes.php file). Then you can get the parameter by doing this in the profile function:
if ($this->uri->total_segments() === 1) {
$profile = $this->uri->segment(1);
}
If you have some other controllers which you would still like to be able to access, e.g. an about us page, add them after the above line in the routes.php file. For example, if we extend the example above:
$route['(:any)'] = 'users/profile';
$route['home/about'] = 'home/about_us';
Would mean that every request is sent to the users/profile function, except http://example.com/home/about will go to the about us page.

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 functions

I have a small problem with routing.
My routes:
$route['category/(:any)/(:num)'] = "site/index/$2"; //not working
$route['category/(:any)'] = "site/index"; //not working
$route['category/(:any)/(:any)'] = "site/view/$2"; // working
$route['Search'] = "site/search"; // working
What I want: http://example.com/category/Home - call site/index function
http://example.com/category/Home/2 call site/index function with parameter $2
I'm geting 404 erro at those 2 rules.
What I tried was to echo the parameter of category/(:any)/(:num) and it echoed it. This echo was inside the index function. The views adn models exists in the paths I declared. The index page itself wouldn't work without it. So I think the problem has to be in routing
The most interesting thing is that when I change the category/(:any) route to site/view it is working but when I set there site/index it is not working. Even if I set there only site .
I think what you are trying to do is to have your site class as "default controller". Just try this :
$route['default_controller'] = "site";
$route['(:any)'] = "site/view/$1";
$route['(:num)'] = "site/index/$1";
I don't really know what you are trying to do with your site/view/$1 and site/index/$1 it will work like this :
example.com/someaction will match $route['(:any)'] and will call the view method of the site controller with someaction as a string parameter.
example.com/2 will match $route['(:num)'] and call the index action of the site controller with 2 as an integer parameter.
example.com/admin will call the index action of the admin controller
example.com/admin/category will call the category action of the admin controller

Categories