Part of the EllisLab Network
   
 
Controller index function params
Posted: 18 July 2008 04:50 AM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  9
Joined  01-28-2008

I tried to user a controller index function with params, but it does not seem to work, says cannot find my page. Am I doing something wrong, or this is was meant to work this way? Why is this not possible?

Profile
 
 
Posted: 18 July 2008 05:56 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  118
Joined  04-05-2007

Show us your code.

Profile
 
 
Posted: 18 July 2008 06:20 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  9
Joined  01-28-2008

class Cart extends Controller {
        
function index($id=0) {

        }
}

/cart - works fine
/cart/20 - does not work, says


404 Page Not Found

The page you requested was not found.

Profile
 
 
Posted: 18 July 2008 06:37 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  94
Joined  03-27-2008

Try /cart/index/20.

Passing URI to Controller (from User Guide)

Profile
 
 
Posted: 18 July 2008 06:41 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  9
Joined  01-28-2008

Yes, but I was wondering why doesn’t it work that way! Since it does not match any other sub from cart controller, it must be a param for index, if index exists.

Profile
 
 
Posted: 18 July 2008 06:51 AM   [ Ignore ]   [ # 5 ]  
Moderator
Avatar
RankRankRankRank
Total Posts:  1767
Joined  07-30-2007

Check out Routing - it will let you do what you need.

$route['cart/(\d+)'] = 'cart/index/$1';

 Signature 

MichaelWales.com

Profile
 
 
Posted: 18 July 2008 07:00 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  9
Joined  01-28-2008

Ok, thanks, is it possible this to become a feature?

Profile
 
 
Posted: 18 July 2008 07:19 AM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2795
Joined  07-14-2006

For your solution you can go two ways. Your way of adding parameters to the index and creating another method to use the parameters in. The second option is mostly used for clarity.

class cart extends controller
{
    
function index()
    
{
       
// landing page
    
}

    
function user($id=0)
    
{

    }
}

So making the index method the default to accept parameters is not always how people prefer to code their apps.

Checking if there is no method in that class with that value as second segment will result in a performance hit because the only way i can think of to do the checking, without changing a config file each time you add a new method, is by preg_matching the class file content.

Profile
 
 
Posted: 18 July 2008 07:22 AM   [ Ignore ]   [ # 8 ]  
Moderator
Avatar
RankRankRankRank
Total Posts:  1767
Joined  07-30-2007

Yeah - I don’t think making the framework automagically figure out that it should be an index parameter is the right way to go about this. The performance hit, first of all.

Secondly, class and method names are not how you define your URL structure. It is a side benefit that the framework uses these to setup a default URL structure - but if you need to define a particular structure, use URL Routing.

Don’t go about naming your classes/methods pretty names for the sake of the URL - the names of classes and methods should be for the benefit of the coder. Routing is for the benefit of the end user.

 Signature 

MichaelWales.com

Profile
 
 
Posted: 19 July 2008 09:21 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
RankRankRank
Total Posts:  316
Joined  08-03-2006

If you add this to your controller, it will work as you described (well, it should, unless I screwed up):

function _remap($method)
{
    
if(method_exists($this, $method))
        
call_user_func_array(array(&$this, $method), array_slice($this->uri->segment_array(), 2));
    else
        
call_user_func_array(array(&$this, 'index'), array_slice($this->uri->segment_array(), 1));
}

If no matching method is found, it calls index instead with all segments.

 Signature 

MPTtree: A model to handle trees in a database.

IgnitedRecord: An ActiveRecord inspired ORM model

YAYParser - Yet Another YAML Parser

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 60658 Total Logged-in Users: 20
Total Topics: 73064 Total Anonymous Users: 0
Total Replies: 393970 Total Guests: 320
Total Posts: 467034    
Members ( View Memberlist )