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'] = '';
Related
$route['default_controller'] = 'Student';
$route['admin'] = 'index.php/Admin_depart/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I am fairly new to CodeIgniter and finishing up my first project. However, before I put it up on my hosting site I would like to clean up the URL's using the routes.php file that CodeIgniter provides in the config folder. but it is not working it work correctly when i write same url i the default_controller
The index.php is causing the issue.
Route destinations must be defined only as a controller/method pair. So in your case the correct route would be:
$route['admin'] = 'Admin_depart/index';
anyone browsing to yoursite.com/admin would be served whatever is in your Admin_depart controller, index method (just as if he/she browsed directly to yoursite.com/admin_depart/index;
I need to remove controller name from URL of codeigniter, for example:
www.example.com/controllername/functionname
www.example.com/welcome/aboutus
www.example.com/welcome/ourservices
I need URL as:
www.example.com/functionname
www.example.com/aboutus
www.example.com/ourservices
Then also i need to have sub menu for our services that URL should look like:
www.example.com/welcome/our services/service1
www.example.com/welcome/our services/service2
How to achieve this routing in codeigniter?
if you ONLY want to remove 'welcome' from the URL,
you could do:
$route['(:any)'] = "welcome/$1"
Go to application/config/routes.php
and add
$route['about-us'] = 'welcome/aboutus';
$route['our-services'] = 'welcome/ourservices';
Make sure your function should not contain spaces. it can be
aboutUs
about_us
public function about_us()
{
# code...
}
This can be achieved By dynamic Routing. Open The file routes.php inside config folder and set the routes.
Example:
$route['default_controller'] = 'Login';
$route['404_override'] = '';
$route['about-us']="Foldername(if-any)/ControllerName/FunctionName";
And to open about-us page u have to give path from controller like
redirect(base_url('about-us'));
Structure of my controller directory:
--Controller
--frontend
--user_controller
When i call default controller it says 404 page not found
Route i have set :
$route['default_controller'] = "frontend/user_controller";
$route['admin_panel/site'] = "site";
$route['admin_panel'] = "backend/admin_controller/dashboard";
$route['admin_panel/login'] = "backend/admin_controller/index";
$route['admin_panel/(:any)'] = "backend/admin_controller/$1";
$route['admin_panel/(:any)/(.*)'] = "backend/admin_controller/$1/$2";
And when i run http://localhost/example/frontend/user_controller then it works.
So but didn't work this url : http://localhost/example
Please try with: move default controller at bottom
$route['admin_panel/site'] = "site";
$route['admin_panel'] = "backend/admin_controller/dashboard";
$route['admin_panel/login'] = "backend/admin_controller/index";
$route['admin_panel/(:any)'] = "backend/admin_controller/$1";
$route['admin_panel/(:any)/(.*)'] = "backend/admin_controller/$1/$2";
$route['default_controller'] = "frontend/user_controller";
Codeigniter will not allow default controller to be in a sub directory.
You have to rewrite it as
$route['default_controller'] = "user_controller";
And place that directly in the controller folder
You can refer this thread to avoid this. But I didn't tested it.
It is better to avoid the core functionalities as it may affect the updates.
I have not tested this but potentially you could use this to get around the sub-folder?
$route['frontend'] = 'frontend/user_controller';
$route['default_controller'] = 'frontend';
need to make sure there is a 'public function index()' in there though as you aren't specifying the method to load...
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
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.