Simple Module Routing to Retrieve 1 Item

edited August 2011 in Modules
I've been building a simple module and one thing has me stumped.

Following the tutorials I have everything setup, but I'm finding that a simple routing pattern site/model/id fails to work. I have my single view file setup with the same name as the model with the uri_segment(2) check at the top, just replacing slug with id.

I can visit site/model, and my list of items gets populated perfectly, but when I try to use a route like this:

site/model/1, I get a 404 and after debugging, it's not even reaching that view page.

My question isn't so much geared toward my specific setup, but more about the recipe (what needs to go where) to get this sort of thing working.

From the tutorial it looks like you need (where model = your plural model name):

A preview_path like so:

config['modules']['model'] = array(
'preview_path' => 'model/{id}'
);

And a new variable file: _variables/model.php

$vars['model'] = array('view' => 'model');

Then of course the matching views etc.


In the tutorial I see: UNIQUE KEY `permalink` (`slug`), is this required? It seems like the only thing I'm missing.

Comments

  • edited 12:17PM
    The preview_path just points to where on the site you can view the module data in your website. It doesn't automatically create the page for you. You'll have to create the page to preview.

    For example, say you have news module for your site. You could create news page that gets the news id past to it at the URI "news/{id}". When you click the view link in the admin, it will go to that link and pass the id of the item. An example of this is near the bottom of this page:

    http://www.getfuelcms.com/user_guide/general/creating-pages
  • edited 12:17PM
    Ok that makes sense.

    I have that setup, and when I click the view link in the admin it goes to the expected url: siteroot/news/10

    But I still get a 404. siteroot/news works, I can retrieve all the items perfectly.

    I added some echo statements to the top of the view page (which looks almost exactly like the one in the link above), and when the id is passed in the url, I am not even getting to the file.

    siteroot/news works
    siteroot/news/10 does not, I don't even make it to my view page.

    Is there something something I need to setup for that to work, or should having the model and the view be enough?

    Here's what my code looks like: http://pastebooth.com/sjIgX (die statements in there for debugging)
  • edited 12:17PM
    The key to get that page to show up is to specify the view in a variables file or set the fuel configuration value of "auto_search_views" = TRUE in your MY_fuel.php file.

    The reason is because if there is no controller set up, it will default to the "opt-in" controller which looks for a view that has the same URI. Since in this case there isn't, you need to specify the view and the best way to do that is probably in the variables file. The very bottom of the page I sent earlier gives an example. You can also use regular expression like in routing for specifying the variable like so:
    $vars['news/:any'] = array('view' => 'news/item');
  • edited 12:17PM
    Ahh, the auto_search_views setting was the key! I definitely overlooked it flipping between the guides

    Thank you very much for explaining everything, I'm sure I'll be back with more questions!

    Collin
Sign In or Register to comment.