Welcome Guest, Not a member yet? Register   Sign In
controller/index/arg ==> controller/arg?
#1

[eluser]err403_love[/eluser]
Is there any way (without routing every URL) to allow CI to default to "controller/argument" if the index() function contains arguments? I don't want to have to set this for every controller in the routes (if you can even change this in routes, that is) and I would find this very useful.

Having to use a URL like controller/index/parameter just doesn't make sense.

I'm not sure if this is really possible though. If I configured it through routes, it would also try to reroute subdirectories, and I can't have that. I just really wish the URL would be "smart" and figure out that if that URI segment is NOT another function in your controller, and it is NOT a subdirectory, to treat it as an argument of the index() function, if it requires arguments, of course.

Thanks.
#2

[eluser]nmweb[/eluser]
Maybe you can figure something out using _remap()
#3

[eluser]Jamie Rumbelow[/eluser]
pre-controller hook?
#4

[eluser]Pascal Kriete[/eluser]
Code:
$segments = $this->uri->segment_array();

$args = array();

// Only do this if we have more than a controller
if (count($segments) > 1)
{
    // Not index - must be an argument
    if ($segments[1] != 'index')
    {
        // Drop controller from array
        $args = array_slice($segments, 1);
    }
    // Index was given for whatever reason
    else
    {
        // Drop controller and index
        $args = array_slice($segments, 2);
    }
}

Change it accordingly if you use folders.

edit-add: it would probably be easiest to extend the url helper with a function that does this instead of doing it every time.
#5

[eluser]chris613[/eluser]
Inparo, I'm not sure I understand your solution. Where would this code go?

I've got a search controller that I'd like to be able to call with /search/terms rather than /search/index/terms or (/search/q/terms)

Anyone know the way to do this? I could probably brew up a solution with apache url rewriting, but some sort of CI based solution would be more elegant.
#6

[eluser]Pascal Kriete[/eluser]
I'm not too sure I understand my code either Tongue . I would probably do it with a router extension now.

For your use case it's easiest with a route, since it's only one controller:
Code:
$route['search/(.*)'] = 'search/index/$1';
#7

[eluser]chris613[/eluser]
Thanks! I've never used routes before, but I think there are a few places on my site that this could be useful. Smile
#8

[eluser]Colin Williams[/eluser]
_remap() _remap() _remap()
#9

[eluser]ray73864[/eluser]
what is this _remap() thou speaketh of???
#10

[eluser]Colin Williams[/eluser]
If you read through the User Guide, you can't miss it. Check out the page about Controllers.




Theme © iAndrew 2016 - Forum software by © MyBB