I am using routing like this
$route['Advertisement/1.0/(:any)']="v1/$1";
$route['Advertisement/1.1/(:any)']="v1_1/$1";
eventually both of them just do same work but i have to maintain both of them because of just response is same.
All is i want to know is how do i get to know the which controller is called using URL .If i get to know the URL like so will change the response accordingly so i don't need to maintain two controllers
1.0 or 1.1
I hope you understand what i am trying to ask.
Thanks in advance.
According to Codeigniter's User Guide, If you want to know the URL which is hit, then use:
$uri_segments = $this->uri->uri_string();
To get the URI segments.
Also, you can use current_url() URL helper to get the full URL (including segments); To do that:
// Load URL helper first (or use autoload config)
$this->load->helper('url');
// Get the current full URL
$url = current_url();
And if you want to get a specific segment of URI, use:
// "n" is the segment number you wish to retrieve,
// in this case, n = 2 gets '1.0' or '1.1'
$segment = $this->uri->segment(n);
Assuming your URL looks like this: example.com/Advertisement/1.0/...
$this->uri->segment(2);
will return 1.0 or 1.1
If i understood correctly, you can get the controller name and method name by using the following CI functions
$this->router->fetch_class(); // to get controller
$this->router->fetch_method(); // to get method
Related
In codeigniter I want to have one url segment to not be consider.
example.com/app/dev1/controller/function/id
example.com/app/dev2/controller/function/id
base url is
baseUrl = example.com/app
When I use this type of url, codeigniter consider "dev1" as controller, "controller" as function, so I get error of page not found.
I want to know if I can code which don't consider first parameter as controller and it start considering from second parameters.
I can not add in base url as it is not constant we can have en, fr, nl etc so do we have anything that help in this case I don't want to add query string "?".
Can we do anything using .htaccess
suppose url is like following : www.paintes.com/painters-in-chennai
then route can be like following
$route['painters-in-(:any)'] ='index/paintersIn/$1';
in index.php controller, i'll be able to receive chennai like following
function paintesIn($city_name)
{
echo $city_name //OUTPUT WILL BE "chennai"
}
if url is like www.paintes.com/painters-in-mumbai , then output will be mumbai
as per your request, URL is liek following : www.paintes.com/mumbai-painters
then you can write like following
$route['(:any)-painters'] ='index/paintersIn/$1';
You have to configure your CodeIgniter routes properly. Example:
$route['dev1/controller/fun/(:num)'] = "dev1/controller/fun/index/$1";
See https://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/
You might want to go for the following in routing
$route['dev1/(:any)'] ='dev1/$1';
$route['dev1/(:any)/(:any)'] ='dev1/$1/$2';
$route['dev1/(:any)/(:any)/(:any)'] ='dev1/$1/$2/$3';
Its rare that Codeigniter treats folder name as controller, try renaming folderas well.
Otherwise above solution should work.
You could add a URI Routing regular expression like this to skip the dev part and go straight to the desired controller :
$route['dev(:num)/([a-z]+)/([a-z]+)/(\d+)'] = '$2/$3/$4'; // rule to match method with parameter
$route['dev(:num)/([a-z]+)/([a-z]+)'] = '$2/$3'; // rule to match method without parameter
Or if you don have a common dev URI segment, you could use :any rule :
$route['(:any)/([a-z]+)/([a-z]+)/(\d+)'] = '$2/$3/$4'; // rule to match method with parameter
$route['(:any)/([a-z]+)/([a-z]+)'] = '$2/$3'; // rule to match method without parameter
we will neeed to create new file "MY_Router.php" in "/application/core" folder, with following content
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Router extends CI_Router {
protected function _parse_routes()
{
// do logic you needed
unset($this->uri->segments[1]);
// Return default function
return parent::_parse_routes();
}
}
I don't know much about the routing concept in codeigniter, I want to pass many parameters to a single method as explained in this http://www.codeigniter.com/userguide2/general/controllers.html tutorial page.
In the url I have this
http://localhost/code_igniter/products/display/2/3/4
In my routes.php I have written
$route['products/display/(:any)'] = 'Products_controller/display';
What I thought is it will pass all the parameters (here 2/3/4) to the method 'display' automatically but I am getting 404 page not found error.
In general I want to achieve something like, if the URI is controller/method I want to route to someother_controller/its_method and pass the parameters if any to that method. How can I do it?
In CI 3.x the (:any) parameter matches only a single URI segment. So for example:
$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';
will match exactly two segments and pass them appropriately. If you want to match 1 or 2 you can do this (in order):
$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';
$route['method/(:any)'] = 'controller/method/$1';
You can pass multiple segments with the (.+) parameter like this:
$route['method/(.+)'] = 'controller/method/$1';
In that case the $1 will contain everything past method/. In general I think its discouraged to use this since you should know what is being passed and handle it appropriately but there are times (.+) comes in handy. For example if you don't know how many parameters are being passed this will allow you to capture all of them. Also remember, you can set default parameters in your methods like this:
public function method($param=''){}
So that if nothing is passed, you still have a valid value.
You can also pass to your index method like this:
$route['method/(:any)/(:any)'] = 'controller/method/index/$1/$2';
$route['method/(:any)'] = 'controller/method/index/$1';
Obviously these are just examples. You can also include folders and more complex routing but that should get you started.
On codeigniter 3
Make sure your controller has first letter upper case on file name and class name
application > controllers > Products_controller.php
<?php
class Products_controller extends CI_Controller {
public function index() {
}
public function display() {
}
}
On Routes
$route['products/display'] = 'products_controller/display';
$route['products/display/(:any)'] = 'products_controller/display/$1';
$route['products/display/(:any)/(:any)'] = 'products_controller/display/$1/$2';
$route['products/display/(:any)/(:any)/(:any)'] = 'products_controller/display/$1/$2/3';
Docs For Codeigniter 3 and 2
http://www.codeigniter.com/docs
Maintain your routing rules like this
$route['products/display/(:any)/(:any)/(:any)'] = 'Products_controller/display/$2/$3/$4';
Please check this link Codeigniter URI Routing
In CodeIgniter 4
Consider Product Controller with Show Method with id as Parameter
http://www.example.com
/product/1
ROUTE Definition Should be
$routes->get("product/(:any)", "Com\Atoconn\Product::show/$1");
Normally in Codeigniter a url by default has two to three segments like this. http://www.yoursite.com/controller/function
My question is, can I replace that function segment for a value if my code is in the index part of the controller.
I have a controller for just user profiles and I'm trying to make that the simplest url possible, so it's easily sharable. For example I would like the url to be something like this http://www.mywebsite.com/author/zazvorniki instead of having to call a function like this. http://www.mywebsite.com/author/user/zazvorniki
Is this possible? Is there a setting I can change to make this possible?
Just add the following route rule.
$route['author/(:any)'] = "author/user/$1";
lets say my url is:
http://www.mysite.com/controller1/
I want to make routing so it wont look for the controller1, it will search for controller1 as the key.
so I will have a key like array for the controllers
<?php
$controllers_mapArr = array(
'controller1'=>'actual_controler_name',
);
?>
it will execute the value of the controller key 'actual_controler_name' but the URL will show controller1
any ideas?
You can easily do this using codeigniter's routing functionality, you have to add rules in application/config/routes.php file, following are the examples for doing
$route['journals'] = "blogs";
if your url is like http://example.com/journals then your URL will call blogs controller
$route['journals/joe'] = "blogs/users/34";
using this rule your if you access your url http://example.com/journals/joe then it will call blogs controller's users method with 34 as a value for function argument.
check the detailed document or routing here : http://ellislab.com/codeigniter/user-guide/general/routing.html
Ok found the answer with a littel help from my friend actually is one of the basics in codeigniter
any way there is the answer:
http://ellislab.com/codeigniter/user-guide/general/routing.html
Is it possible to create such urls in codeigniter?
http://site.com/shorturl/
Where shorturl isn't a physical controller file, but a variable.
I expect the algorythm for parsing url query to be like this:
1) Search for physical controller file. If exists, do standard codeigniter routine. If not
2) Try to load special controller file, where "shorturl" is a variable. Do further stuff inside that controller.
Thanks in advance
The previous answer seems quite good, but thought I'd share what I'd though of.
If you set your 404_override to point to a controller you have set up as follows:
$route['404_override'] = 'welcome/short';
Any URL that doesn't exist (any short URL for example) would get sent there, where you could do the following to check the value:
public function short() {
$shortCode = $this->uri->segment(1);
}
That would give you the value you need to check. If all is well, do the redirect, if the code doesn't exist, you can then use the show_404 method to actually show the 404 page.