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();
}
}
Related
i want to achieve url routing something like www.example.com/alicia
suppose alicia is not a class name or method name just something like passing data in url and with some class i want to access this and want to use it for further process.How i can use it ? Thanks in advance.
You can use Codeigniter's built-in routing, the file route.php is located in your config folder.
there you can add:
$route['alicia'] = 'welcome/index/something';
$route['alicia/:any'] = 'welcome/index/someotherthing/$1';
then in your controller, for example welcome you just create a function like:
public function index($page = null){
if($page=='something'){
// do what you need to do, for example load a view:
$this->load->view('allaboutalicia');
}
elseif ($page=='someotherthing'){
// here you can read in data from url (www.example.com/alicia/2017
$year=$this->uri->segment(2); // you need to load the helper url previously
}else{
// do some other stuff
}
}
documentation on routing and on urlhelper
edit after comment:
in case your uri segment is representing a variable, like a username, then you should use a uri scheme like www.example.com/user/alice and create your route like:
$route['user/:any'] = 'welcome/index/user';
then in your controller welcome
public function index($page=null){
if($page=='user'){
// do what you need to do with that user
$user=$this->uri->segment(2); // you need to load the helper url
}
else{
// exception
}
}
This could be tricky because you don't want to break any existing urls that already work.
If you are using Apache, you can set up a mod_rewrite rule that takes care to exclude each of your controllers that is NOT some name.
Alternatively, you could create a remap method in your base controller.
class Welcome extends CI_Controller
{
public function _remap($method)
{
echo "request for $method being handled by " . __METHOD__;
}
}
You could write logic in that method to examine the requested $method or perhaps look at $_SERVER["REQUEST_URI"] to decide what you want to do. This can be a bit tricky to sort out but is probably a good way to get started.
Another possibility, if you can think of some way to distinguish these urls from your other urls, would be to use the routing functionality of codeigniter and define a pattern matching rule in the routes.php file that points these names to some controller which handles them.
I believe that the default_controller will be a factor in this. Any controller/method situations that actually correspond to a controller::method class should be handled by that controller::method. Any that do not match will, I believe, be assigned to your default_controller:
$route['default_controller'] = 'welcome';
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");
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";
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.
So redmine has a very peculiar url mapping style that i observed :
http://demo.redmine.org/projects/<project-name>/controller/action
samples :
http://demo.redmine.org/projects/produto/activity
http://demo.redmine.org/projects/produto/issues/new
http://demo.redmine.org/projects/produto/issues/gantt
http://demo.redmine.org/projects/produto/files
and the url changes as the project changes.
how do i do this in codeigniter ? I'm thinking it can be done with routes.php but so far i'm not able to get anywhere.
Looking for any help. Thanks.
Use the following function inside your "application/controllers/projects.php" controller:
public function _remap($method)
{
if ($method == 'project-name')
{
//display project1
}
elseif($method == 'project-name2')
{
//display project2
}
}
You can do the same for varying methods by extracting them from database
take a look here:
http://codeigniter.com/user_guide/general/controllers.html#remapping
you can also route your controller by using custom routes in application/config/routes.php
$route['example'] = "controller/function";
$route['example2/(:any)'] = "controller/function";
You use the routes file in application/config/routes.php
You would use something like this:
// the $1 maps to :any
$route['projects/produto/:any'] = "$1";
// the $1 maps to the first any, $2 maps to the second :any
$route['projects/produto/:any/:any'] = "$1/$2";
You will want mod_rewrite enabled if you are handling clean URL's. Otherwise expect the index.php/controller/action. I cant test it myself there, but you should refer to:
Once you add a route (It has to be called $route[] inside the configuration), refresh the page and try to go to the URL!
http://codeigniter.com/user_guide/general/routing.html
add this to your routes.php (btw: you need url-rewriting enabled for routes to work, ie. using .htaccess)
$route['projects/(:any)/(:any)/(:any)'] = "$2/$3/$1";
for example /projects/produto/issues/new will call the function new in the class issues and pass it the parameter 'produto'
also check http://codeigniter.com/user_guide/general/routing.html