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
Related
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'));
I want to remove controller name from the URL
My Controller name is = admin
My Current URL is
http:///myaliveidea.com/usermanagement/admin/dashboard
But I want to make my URL like this
http:///myaliveidea.com/usermanagement/dashboard
please help......
You need to make a route in config file, so add this.
$route['usermanagement'] = 'admin/dashboard';
This will route to your dashboard controller in admin folder.
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
Can anyone help me?
I am new in codeigniter and currently working in a project.
I have a controller named Articles with the following methods:
index()
page($page)
category($category)
view($slug)
I want to change my urls as follow:
from http://site.ph/articles/page/<page> to http://site.ph/articles/<page>
http://site.ph/articles/category/<category>/<page> to http://site.ph/articles/<category>/<page>
and
http://site.ph/articles/view/<slug> to http://site.ph/articles/<category>/<slug>
Thanks!
U can use URI Routing for this pourpose...
U need to go to the application/config/routes.php file and add the following array's in the file
$route["articles/$page"] = "articles/page/$1";
$route["articles/$category/$page"] = "articles/category/$1/$2";
$route["articles/view/harold-paola"] = "articles/$1/$2";
for more details on uri routing check this link
http://ellislab.com/codeigniter/user-guide/general/routing.html
I have controller named “profile” and the URL for user profile is
www.example.com/profile/user
I am already using the rerouting of codeigniter in the routes file
$route['profile/(:any)'] = "profile/index";
what I am looking is to remove the profile controller name from the URL so it will be SEO and user friendly.
e.g
www.example.com/user
any suggestions?
You may try any one of these
// url could be yourdomain/imran
$route['(:any)'] = 'profile/index/$1';
// url could be yourdomain/10
$route['(:num)'] = 'profile/index/$1';
// url could be yourdomain/imran10
$route['([a-zA-Z0-9]+)'] = "profile/index/$1";
Your class may look like this
class Profile extends CI_Controller {
public function index($id)
{
// $id is your param
}
}
Update : (Be careful)
Remember that, if you have a class Someclass and you use url like yourdomain/Someclass then this will be routed to profile/index/$1 if you have $route['(:any)'] or $route['([a-zA-Z0-9]+)'].
You can use this in a route file:
$route['(:any)'] = "controller_name/$1";
$route['(:any)/(:any)'] = "controller_name/$1/$1";
$route['(:any)/(:any)/(:any)'] = "controller_name/$1/$1/$1";
Try this:
$route['user'] = 'profile/user';
Dont know any generic way to do it
Possible duplicate of How to hide controller name in the url in CodeIgniter?
If I understand your question correctly, you just want to add a catchall to the very end of your routes file. Like:
$route['(:any)'] = 'profile/index';
This will catch everything that isn't caught by another route, so make sure this script contains logic to determine when a 404 should be presented.