Simple Module Routing to Retrieve 1 Item
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
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
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)
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');
Thank you very much for explaining everything, I'm sure I'll be back with more questions!
Collin