Welcome Guest, Not a member yet? Register   Sign In
passing url vars to controller
#1

(This post was last modified: 03-02-2015, 09:39 AM by geegee.)

i'm am very new to the whole mvc thing and i followed the CI tutorial.

that seems sort of straightforward.

but instead of calling an just a method with params how do you do something like this:

ww.example.com/index.php/categories/3/subcategories/4/items/5

so i want to show item number 5 form subcategory 4 belonging to category 3.

i have seen this in a ruby on rails application which also uses mvc and i wondered how that can be implemented with CI.

do i create a single method called "categories" which accepts " 3, 'subcategories' , 4, 'items' and 5" all as 5 seperate parameters or is there a better or efficient alternatieve to this?
Reply
#2

Suggestion: Show controller, with item method taking 3 parameters.

class Show extends CI_Controller {
...
function item($category=null,$subcat=null,$item=null) {
// if null category, show list of categories
// else, if null subcat, show subcats in category
// else, if null item, show items in subcat
// else show the requested item
...
}

The URL would then be www.example.com/show/item/5/4/3

Not a perfect solution, but less awkward.
Reply
#3

hmm i think what i was looking for is this:
https://ellislab.com/codeigniter/user-gu...s/uri.html

with that you can decide yourself which segments in your url you can do with what you like.
Reply
#4

geegee, you might want to start using CI version 3.0, rather than the older version 2.x.  The docs for this are on the CodeIgniter site (not Ellislab.com)

This is a link to the current User Guide page for the uri class:

http://www.codeigniter.com/userguide3/li...s/uri.html

The syntax is similar but there are changes with version 3.0.

Unless you are supporting an existing set of code written for Ver 2.x, do start using the latest CI version.  It is very stable, in my experience, and you will be ahead of the curve if you learn CodeIgniter 3.0, rather than the earlier version.
Reply
#5

building on what others have written

link:
http://www.example.com/category/books/mysteries/5

config/route:
$route['category/(:any)/(:any)/(:num)'] = "show/category/$1/$2/$3" ;

controller: Show

method: category($category=null,$subcat=null,$item=null){ // blah blah }

note: there are no numbers in the url except for the item number.
we don't reveal the controller name in the URL.
the URL is short and clear.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB