Codeigniter Routing - php

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

Related

How to route the URI with parameters to a method in codeigniter?

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

codeigniter how do i setup routing for controller class and method?

I am new to CI and need some beginner's help from experts.
Here is what my current setup is:
/controllers/
home.php
report.php
/views/
home/index.php
home/recent.php
report/index.php
report/generate.php
the URI i am trying to produce as an outcome:
http://localhost
http://localhost/report (would load the index.php)
http://localhost/report/generate (would call the method for generate in the report controller)
http://localhost/recent/10 (would call the method for generate in the home controller passing the variable '10')
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['/'] = 'home/index';
$route['recent/(:num)'] = 'home/recent/$1';
$route['report/(:any)'] = 'report/$1';
How do i avoid always modifying the routes file for each new method created in a class? so that it would follow:
$route[$controller/$method/$variable] (very use to how .net mvc routing is setup).
Any help is appreciated.
You don't need further modifications. In fact, even this line is redundant:
$route['report/(:any)'] = 'report/$1';
This one is also redundant:
$route['/'] = 'home/index';
since the default controller is set to 'home' and the default method is always index.
Look at how CI works with URLs: https://www.codeigniter.com/user_guide/general/urls.html
So, /localhost/report/generate would look for the Report controller, and load its generate method. That's how it works out-of-the-box, no routing needed.
And this route is fine:
$route['recent/(:num)'] = 'home/recent/$1';
If will take the URL /localhost/recent/123 and will load the Home, controller, recent method and will pass 123 as the first method parameter.

How to pass value/variable directly to default controller in codeigniter?

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.

codeigniter routing functions

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

Using any other controller class rather than the default controller class

I am working with CodeIgniter. Here's my routing file
$route['default_controller'] = 'pages/view/home';
$route['(:any)'] = 'pages/view/$1';
where
pages is the controller class and view is a function of it and home is a parameter to that function.
Now, this is the default controller. When i need to open someother page rather than 'home' I do it like as follows from inside a view
href="<?php echo base_url('products');?>
Now what i want to ask is, if i create a new controller, how can i use the function of that controller? since I am only passing the third parameter to the base_url() function.
Obviously I think I gotta write $routes, but how ? since all the traffic is passed to
pages/view
I tried creating a new controller but couldn't be able to use it. My new contoller was name new_controller and it has a function call new_function()
and I wrote the $route as follows
$route['pages/view/product'] = 'new_controller/new_function';
You shouldn't have to worry about Routes if you take away the (:any) route you have place there. That is blocking all other controllers from being loaded, I think.
If you have a controller called "Stuff"
in your URL when you have mysite.com/stuff/foo/param Code Igniter should bypass the default "page" controller and use
I think you would be better off doing something like this
$route['page/(:any)'] = "page/view/$1";
And change your default to be only 'pages'
That would open up your new controller to be used in the normal codeigniter fashion
In CodeIgniter the routes are evaliated in row, so first you have the default route and after that sould you place the new route, $route['pages/view/product'], if you want to keep the (:any) route, and with this, you place the exceptional routes before the (:any) route.

Categories