• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Routes and RegEx

#1
I've got a question about routes. My controller has an index function that is used for showing a list of items, and some other functions for creating, editing and deleting items.

The url mysite/manage_products triggers the index function.
Now, on the list of items, I've got buttons for multiple categories, let's say: All, New and Used Products.

In my routes.php I have:
PHP Code:
$route['manage_products/all'] = 'manage_products/index/all';
$route['manage_products/new'] = 'manage_products/index/new';
$route['manage_products/used'] = 'manage_products/index/used'

How can I shorten this to one route?
The url manage_products/create should NOT trigger the index function, so I can't have:
PHP Code:
$route['manage_products/(:any)'] = 'manage_products/index/$1'
Reply

#2
It won't trigger index(). It will look for a matching function before using that specific route.
I got the following, and I can still use folder/create, folder/update etc.
PHP Code:
$route['folder/(:any)']             = 'folder/index/$1'
Reply

#3
Tested it, but it's not working! All URL's that start with manage_products/ are routed to the index function.
Reply

#4
You need to show us some more code then. Somewhere you are modifying it so it's not working.

1. Create a new class with only index() and create() function, and echo out different text.
2. Delete everything in your route and place only one route.
3. Profit.

If not. Start with a fresh Codeigniter. Because it's going to work. :-)
Reply

#5
My controller:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Products extends CI_Controller {
 
    
  public 
function index($category='all')
 
 {
 
     echo 'Category: ' $category;
 
 }
 
 
 public function create()
 
 {
 
    echo 'The create() method is called...';
 
 
 


My routes.php:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'pages/view';
$route['products/(:any)'] = 'products/index/$1'

Results:
mysite/products ==> Category: all
mysite/products/all ==> Category: all
mysite/products/new ==> Category: new
mysite/products/create ==> Category: create

So all URL's are routed to the index method, dispite the fact that a "create()" function exists.
Reply

#6
My bad. I always specified id after my /folder/create/id.
You can do the same or define a static page.
PHP Code:
$route['products/create'] = 'products/create'// or make your urls like this /products/create/product/
$route['products/(:any)'] = 'products/index/$1'

Or use _remap() without anything configured in $route.
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Products extends CI_Controller {

    public function 
index($category='all')
    {
        echo 
'Category: ' $category;
    }

    public function 
create()
    {
        echo 
'The create() method is called...';
    }

    public function 
_remap($method$params = array())
    {
        if (
method_exists($this$method)) {
                return 
call_user_func_array(array($this$method), $params);
        } else {
            if( empty(
$params) ) { $params[] = $method; }
            return 
call_user_func_array(array($this'index'), $params);
        }
        
show_404();
    }
}
  
Reply

#7
Thank you! The _remap() function was always a mystery to me. Now it's clear. Thanks.
Reply

#8
I'm not sure if this is a possibility, so first forgive me if this is a redundant suggestion, but now I save all routes and their remapping in a database, and then I simply loop through them in the route config file. It's so convenient rather than having to worry about regex, it might not be practical for your case though.
Practical guide to IgnitedCMS - Book coming soon
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.