Welcome Guest, Not a member yet? Register   Sign In
URI segment in index function?
#1

[eluser]Sam Granger[/eluser]
Hey guys!

I have a category class controller.

Code:
<?php
/**
* @author Sam Granger
* @copyright PHPSauce, 2009
*/

class Category extends Controller {

    function Category()
    {
        parent::Controller();
    }
    
    function index()
    {
        echo $this->uri->segment(2);
    }

}

?>

Currently if I go to http://domain.com/index.php/category/2 I get a 404. What am I doing wrong?
#2

[eluser]xwero[/eluser]
If there are no segments the url routes to the default controller, in the routes.php file, and the default method, hardcoded in the router. This works fore a single segment too but from the moment two segments are found the router assumes the first segment is the controller and the second the method.

So to make your url possible you need to use routing. If the controller has only one method then you can do
Code:
$route['controller/(.+)'] = 'controller/index/$1';
Or you can use this as a catch all route for the controller.
#3

[eluser]TheFuzzy0ne[/eluser]
...Or you can call your URL like this:

http://domain.com/index.php/category/index/2 (Yuck)

I would suggest that you create another method, perhaps show_category or something like that, and use the index method to alias it. Basically, it will only alias it when there are no arguments, but from then on, the user will be using show_category.

Code:
function index()
{
    // This is the first page the user sees (it's called without any arguments)
    $this->show_category();
}

function show_category($id=FALSE)
{
    // All of your logic goes in here
}
#4

[eluser]xwero[/eluser]
TheFuzzy0ne http://domain.com/index.php/category/show_category/2 looks even uglier. There are two options to make the url work, with routes or with _remap. _remap is basically routing on class level.




Theme © iAndrew 2016 - Forum software by © MyBB