Passing variables before controller in CodeIgniter - php

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

Related

Codeigniter routing the proper way

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');

How to pass parameters to the index function of default controller in Codeigniter

In my Codeigniter project, I need to generate URLs like this example.com/username. Here 'username' is a parameter to the default controller 'Home'.
I am using remap to pass params to the default controller's index function.
function _remap($param)
{
$this->index($param);
}
I am working on a CodeIgniter project. I have a default controller as 'Home'.
My site URL is example.com. What I need to do is pass the parameter to the default controller 'Home' so I can show profiles like this:
example.com/username
But I can't use this URL. The only way I can pass the parameters to the default controller and make it work is to show the default controller in the URL.
like example.com/home/username. Which is not what I need.
I need the default controller not to show in the URL and generate URLs like example.com/username.
index() in the default Home controller will be like
public function index($params = ''){
echo $params;
}
calling index() here
function _remap($param)
{
$this->index($param);
}
in your config/config.php file. define your url.and then define route for parameters.
$config['base_url'] = 'example.com';

I want to remove controller name from url in every controller is it possible?

I want to remove controller name from every controller is it possible to do like this in route file.For single controller it is working fine for me what if I want to use for all controller in my project?
$route['default_controller'] = 'user';
$route['(:any)'] = "user/$1";
$route['(:any)/(:any)'] = "user/$1/$1";
$route['(:any)/(:any)/(:any)'] = "user/$1/$1/$1";
$route['(:any)'] = "grant/$1";
$route['(:any)/(:any)'] = "grant/$1/$1";
$route['(:any)/(:any)/(:any)'] = "grant/$1/$1/$1";
when I tried this its not working for me.
try this
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]+)'].
I got the answer
$route['(:any)'] = "user/$1"; //this is dynamically for user controller
for another controller in static way like this
$route['functionName'] = "controller/functionName";
$route['functionName'] = "controller/functionName";
this work fine for me for the time .I wish if this is possible dynamically
The code example you have provided will not work because the rules are exactly the same, for example:
$route['(:any)'] = "user/$1";
$route['(:any)'] = "grant/$1";
There is no difference between the two so how is the application going to know where to route your request. It will just pick the first route that matches for all requests (user controller)
What you are trying to achieve is not possible unless (as #syed) has tried explaining above, there is a difference in at least the secondary parts of the url.
For example any numeric ids are passed to a user controller.
// url could be yourdomain/10
$route['(:num)'] = 'user/index/$1';
Any text based are passed to a different controller
// url could be yourdomain/imran
$route['([A-z]+)'] = "name/index/$1";
So on so forth.

How to remove controller name from the URL in codeigniter?

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.

Codeigniter Routing

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";

Categories