I want to understand how codeigniter routing works.
i know that codeigniter interprets url as www.example.com/class/function/id. I have defined this url below but it returns me to the page where I am currently at. say am at about.php page , it returns me to the same page
$route['webadmin/teacher/class/subject/(:num)/(:num)'] = "admin/teacher_details/$1/$2";
in my admin controller i have defined teacher_details as
public function teacher_details($a='', $b = '', $c ''){}
what i want is for the defined url to render as www.example.com/webadmin/teacher/class/subject/id1/id2 for the controller admin/teacher_details
codeigniter support segment based url, not query like
in we pass url as domain/controller/funtion_name/parameter.
suppose you have created a controller named demo with function demo_function
class Demo extends CI_Controller
{
public function demo_function($id='')
{
echo "demo"
}
}
then you will create route in config/route.php
$route['demo/(:num)'] = 'demo/demo_function';
you can access parameter of url like $this->url->segment(3)
or $this->input->get('parament_name');
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 have multiple controllers in a Codeigniter project. I need to hide these controller names. For eg: I have two controller, home and school. Each school have their own page which includes about,gallery,contact etc. and url should be http://www.sitename.com/schoolname. I hide home controller using routes.php.
$route['(:any)'] = 'home/$1';
But it shows error in school controller. Please help me..... Thanks.
It is not true that a controller name is absolutely required.
In routes.php first define default controller to be home (when user accesses homepage sitename.com)
$route['default_controller'] = 'home';
Then you create the rule that any other page should be redirected to school controller:
$route['.*'] = 'school';
Now the home.php controller will look like this:
class Home extends CI_Controller {
public function Index()
{
echo "This is the homepage";
}
}
And in the school.php controller you have to manually get the name of the school from requested URL:
class School extends CI_Controller {
public function _remap()
{
echo "User requested school: " . $this->uri->segment(1);
}
}
Why use _remap method? Because it will be called everytime regardless of what's in URL or routing.
From the docs:
If your controller contains a method named _remap(), it will always get called regardless of what your URI contains.
try this
$route['anything you want/(:any)'] = 'home/$1';
controller name is required so set it with any name you want
I am working on Codeingiter and have a Function in one of my class.
class Search extends My_Controller{
public function index(){
//my search code here...
}
}
Now for this function i need set a route so i can make the URL look like
http://example.com/s/?q=searchtext
or
http://example.com/s/#q=searchtext
However when i do this :
$route['s/?q=(:any)'] = "search/index";
$route['s/#q=(:any)'] = "search/index";
It does not work and i am being redirected to 404 page.
It seems CI does not allow this kind of Routing. Please tell me how i can do this.
Also If i use http://example.com/s/#q=searchtext
how can i get the value searchtext in my function ?
I want to pass some variables to my URL before controller name
let's suppose my URL looks like http://mysite/controller/function/etc/etc
and I want to call it as http://mysite/username/controller/......
I have tried the solutions here but it did not work for me.
my routes.php looks like
$route['default_controller'] = "index";
$route['(:any)/index'] = "index/index/$1";
$route['(:any)/test'] = "test/page/$1";
this some how solves my problem but I have to pass the controller name for the default controller as well.
Is there any way to do like if I go to http://mysite/username, it should go to default controller???
Controller = Index, Function = getUser
Change routes to
$route['(:any)/index'] = "index/getUser/$1"
And add to index controller
public function getUser($username) {
//return user's page html
}
This assumes url style of
http://www.example.com/username/index