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.
Related
I've been researching up on how to add views and I'm stumped. I want to add a view and all I get are 404 errors. All the examples I see on the web are just to add a default controller. I have a default controller, now I want to add a new page passed an ID in the URL.
This is the controller xyz.php:
class Xyz extends CI_Controller {
public function index() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_main_view');
}
public function activity() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_activity_view');
}
}
Model is xyz_model.php, and views are xyz_main_view.php and xyz_activity_view.php.
This is routes.php:
$route['default_controller'] = 'xyz'; // works okay
// $route['xyz'] = 'xyz/activity'; // 404
// $route['activity'] = 'xyz/activity'; // 404
// $route['xyz/activity'] = 'xyz/activity'; // 404
// ... many, many other different approaches
I'm able to use http://localhost, but I'd like to use the following:
// map to main view
http://localhost/index
http://localhost/xyz
http://localhost/xyz/index
// map to activity view
http://localhost/activity
http://localhost/xyz/activity
My understanding is that some of the URLs for the main view should work automatically, not seeing it. Just http://localhost.
I haven't even touched how to get an ID from the URL for the activity page. Just want to get over this first hurdle.
Keep this code in routes
$route['default_controller'] = 'xyz';
Then Try this URL to execute "activity()" function in xyz controller.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity
To Pass Parameters such as id's you can use this url.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity/[ID]
For more information please check codeigniter routing library. It is really easy to understand.
https://www.codeigniter.com/user_guide/general/routing.html
I have a codeigniter application in my localserver.
I need some URL rewriting rule for the following.
If any one have any solution please answer.
http://localhost/myapp/index.php/users/login
to
http://localhost/myapp/index.php/usuarios/login
How can I do this in Codeigniter.
This record in config/routes.php will do what you want -
$route['users/login'] = 'usuarios/login'; // for login only (static)
$route['users/(:any)'] = 'usuarios/$1'; // for all routes to users (dynamic for functions without parameters)
More details here : URI Routing
Codeigniter works like this to access myFunction from Mycontroller with the value myVarValue
http://my.webPage.com/index.php/MyController/myFunction/myVarValue
U want to alter the URL, yes you can by creating routes on ../application/config/routes.php
$route['my-function-(:any)'] = 'MyController/myFunction/$1';
This is the first step
You created the routes and those are not working?
Set the base_url on the file ../application/config/config.php, in your case will be http://localhost/myapp, line number 26 of the file on CI v3.1.6
Set the default controller, the default controller that will be inmediatelly accessed by anyone who enters ur website, u set this on ../application/config/routes.php, your default controller can be the name of a Controller or A function within a controller.
Let's say u have this controller
public class MyController extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
echo 'Hello people, im the index of this controller,'.
' im the function u will see everytime u '.
'access the route http://localhost/myApp/MyController';
}
public function notTheIndex(){
echo 'Hello, im the function u will see if u'.
'access http://localhost/MyController/notTheIndex';
}
}
Lets say this controller will be ur default controller so as mentioned in the routes.php file in the line 53 on ci v3.16 set
$route['default_controller'] = 'MyController';
//if u want to just access the index method
$route['default_controller'] = 'MyController/notTheIndex';
//if for some reason u have a method u would like to be executed
//by default as the index page
Now u want the uris to keep have the names u set
$route['some-beautiful-name'] = 'MyController/notTheIndex';
so when u access http://localhost/some-beautiful-name it will show the result of MyController/notTheIndex
Consider that if u are accessing the different pages via a <a href='some_link'>Some link</a> and the href values need to be changed to your defined routes, if not, this will still work if they are pointing to controllers, but the uri names will stay the same, lets take the example of 'some.beautiful-name' to access MyController/notTheIndex
In Your View
This is a link
The link will work but the uri name will be http://localhost/MyController/notTheIndex
This is a link
The link will work because u defined a route like this else it will not and show 404 error, but here the url shown will be http://localhost/some-beautiful-name
Hope my answer helps u.
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
I'm trying to write an application that takes a parameter from the url and supplies it to the index function in the default controller. Then decides what to load depending on this parameter either a default home page or a custom page.
$route['(eventguide/:any)'] = 'eventguide';
This code works but only if I have the controller in the url like so:
example.com/eventguide/(parameter)
I don't want to include the controller name. So i'm not exactly sure how to route this.
Ideally the url would look like example.com/(parameter), is this possible?
Yes, you're almost there:
$route['(:any)'] = "eventguide/index/$1";
And in your index() method you fetch the parameter:
public function index($parameter = null){
}
$parameter will now contain anything caught by the :any shortcut, which should be equivalent to (\w)+ IIRC
Since this is a catch-all route, be careful to put any other custom route you want before it, otherwise they will never be reached.
For ex., if you have a controller "admin", your route files should be;:
$route['admin'] = "admin";
$route['(:any)'] = "eventguide/index/$1";
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