Welcome Guest, Not a member yet? Register   Sign In
Passing variables via URI segments to controller index()
#11

[eluser]Mahmoud M. Abdel-Fattah[/eluser]
[quote author="jedd" date="1236989181"]EDIT: - Far out - didn't realise this dated back a year. Sorry. My question-as-an-answer remains for the me too person, though.

Quote:Ah, so I wasn't doing anything wrong; this functionality just isn't built in to the index for some reason. Any explanations to why so?

Out of curiosity, what algorithm would you use if you were trying to implement this yourself?

Given code (rough) like this in the controller called Foo:
Code:
function  index ($alpha, $bravo)  {
     echo "Your primary and secondary fruits are : ". $alpha . $bravo;
     }

function  apple  ($alpha, $bravo)  {
     echo "Apple says: ". $alpha . $bravo;
     }

If you hit the web site with a URL localhost://foo/apple/blueberry

What output would you expect to see? What output would you want to see? Which output would be the least/most annoying?[/quote]

For me, I want to make http://mywebsite/item/item-name/
so, I created a controller called item, now I want to get the item-name details from the DB, so it should be processed within index() !

So, how can I make this ?

/**
* EDITED : Solution found
**/

Code:
function index() {
        $data = array();
        $data['title'] = "TITLE";
        $this->load->vars($data);
        $this->load->view('template');
    }
    
    function _remap() {
        echo $this->uri->segment(2);
    }
#12

[eluser]Mahmoud M. Abdel-Fattah[/eluser]
I've now, another question what if I want to call a function within this controller. for example :

Now when I visit :
http://mywebsite/controller/segment1/segment2/segment3/
the controller load _remap(), and pass segment1, segment2 & segment3 to it.

I want , when I visit :
http://mywebsite/controller/submit/Segme.../Segment3/
calls submit() function, and pass segment1, segment2 & segment3 to it.

Hope you get it !
#13

[eluser]jedd[/eluser]
Quote:Hope you get it !

I'm not really sure that I do.

I think my reticence to use routing is based partly on the added level of confusion it'll introduce to troubleshooting. There's probably also some reflection on my design, that I have controllers with several related methods, that means I haven't had to try to subvert the URL design. But maybe I just haven't done anything sophisticated enough to have come up across a problem that has routing as a solution.


What I don't get is that if you're willing to have a URL this long:

Code:
http://mywebsite/controller/segment1/segment2/segment3/
.. why you'd put this much effort into removing, say, three characters from it.
#14

[eluser]Mahmoud M. Abdel-Fattah[/eluser]
cause it'll be a directory, so it may be :
http://mywebsite/browse/category1/

OR
http://mywebsite/browse/category1/sub

etc.

u mean, as it may be long, why not using /browse/category(method)/category1(segment) ??

BTW, I thought of another thing, but I'm not sure regards performance !

Code:
function _remap() {
    $segment_2 = $this->uri->segment(2);
    if($segment_2 === "submit") {
        $this->submit_new_category();
    } else {
        $this->get_categories();
    }        
}
#15

[eluser]jedd[/eluser]
Quote:u mean, as it may be long, why not using /browse/category(method)/category1(segment) ??

Something like that, yeah.

I take the view that this stuff can be quite complex already, without introducing more complexity. By the time I have so many controllers (in my case that'd be upwards of 20) that I need to put them into directories, then I'll have hopefully a good idea on how to do that.

My question from above is applicable again with your latest remap example - what happens, say, if the first parameter someone passes to you is the string 'submit'?

Too much complexity for my liking.
#16

[eluser]Mahmoud M. Abdel-Fattah[/eluser]
ok jedd, simply am calling the functions by checking the URL 1st.

So, when the class loads, I'll check for uri segment:
if it OPTION_1
call function_1,

if OPTION_2
call function_2

else
call function 3

is it clear now ?!
#17

[eluser]jedd[/eluser]
You do realise that the CI framework does this already, yeah?

It takes the first segment, and sends you to that controller. It takes the second segment, and sends you to that method within the controller. I'm not sure what you're trying to gain by subverting this approach in this particular instance.

I refer you again to my original [url="http://ellislab.com/forums/viewreply/547558/"]question above[/url] on how you resolve segment/method name clashes.
#18

[eluser]naren_nag[/eluser]
[quote author="jedd" date="1237146466"]You do realise that the CI framework does this already, yeah?

It takes the first segment, and sends you to that controller. It takes the second segment, and sends you to that method within the controller. I'm not sure what you're trying to gain by subverting this approach in this particular instance.

I refer you again to my original [url="http://ellislab.com/forums/viewreply/547558/"]question above[/url] on how you resolve segment/method name clashes.[/quote]


To answer your question: You make all the methods private.

Here's how I'm using it. I've got a controller called Tag. Now when I call mysite/tag/codeigniter I want to find everything tagged with codeigniter. But when I call mysite/tag/scripts/codeigniter I only want to find scripts tagged with codeigniter. If I just call mysite/tag/scripts I get redirected to the main scripts display page.

Here's my remap function:

Code:
function _remap() {
    
    $this->tag_or_entry_type = $this->uri->segment(2);
    $this->tag = $this->uri->segment(3);
    
    $this->index();
}

And in my index function I run a simple switch case on $this->tag_or_entry_type, where I check if the user is trying to find articles etc, but as my default I'm calling a private search function that returns articles, scripts and downloads tagged with the tag passed.

This is a bookmark friendly way of setting up intuitive URLs for users, especially where search is involved.


cheers,




Theme © iAndrew 2016 - Forum software by © MyBB