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
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';
I'm new in Codeigniter framework and got routing problem.
I have Main controller and Tournaments controller.
My routes look like this:
$route['default_controller'] = 'main';
$route['main'] = 'main';
$route['tournaments/results/(:num)'] = 'tournaments/results/$1';
When I go to localhost/tournaments/results/1 and then click to link "Main" I got localhost/tournaments/results/main instead of localhost/main
What is wrong with my routes or maybe problem is somewhere else?
Probably your link to 'Main' is wrong, not your routes files. Check your link.
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 am working on a codeigniter app and am having some trouble wrapping my head around a routing issue. Basically I would like all routes to map to an specific controller action by default, but I would also like to able to specify an array of routes (or ideally initial url segments) which shouldn't follow this pattern.
By way of example:
If I enter domain.com/username it maps to domain.com/controller/method/show/username
If I enter domain.com/account it maps to domain.com/account
Any help very gratefully received!
James
Open config/routes.php and add the following:
$route['(:any)'] = "controller/method/show/$1";
Please see the link below for more routing concepts.
http://codeigniter.com/user_guide/general/routing.html
Routes will run in the order they are defined. So in your routes file, put the routes for other controllers you still want to work above your catch-all for usernames:
$route['default_controller'] = 'home'; //so root url still works
$route['accounts'] = "accounts";
$route['accounts/(:any)'] = "accounts/$1";
...
$route['(:any)'] = "controller/method/show/$1";
I'm hoping that this will be a simple question that someone can answer. I'm looking to build a CodeIgniter application that I can build pretty easily in regular PHP.
Example: I would like to go to http://locahost/gregavola and have rewritten using htaccess file to profile.php?user=gregavola. How can I do this in CodeIgniter?
Usually in htaccess I could write ^(\w+)$ profile.php?user=$1 but that won't work with the paths in CodeIgniter.
Any suggestions?
CodeIgniter turns off GET parameters by default; instead of rewriting the URL to a traditional GET style (IE, with the ?), you should create a user controller and send the request to:
http://localhost/user/info/gregavola
Then in the user controller, add the following stub:
function info($name)
{
echo $name;
}
From here you would probably want to create a view and pass $name into it:
$data['name'] = 'Your title';
$this->load->view('user_info', $data);
You can find all of this in the CodeIgniter User Guide, which is an excellent resource for getting started.
To map localhost/gregavola to a given controller and function, modify the routes file at application/config/routes.php like so:
$route['(:any)'] = "user/info/$1"
Routes are run in the order they are received, so if you have other routes like localhost/application/dosomething/, you will want to include those routes first so that every page in your entire app doesn't become a user page.
Read more about CI routes here: http://codeigniter.com/user_guide/general/routing.html
Good luck!