Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
router Method
  • Hey everyone, I am new to FuelPHP.
    I have a little trouble understanding how to properly use the "router" method of the Controller class.

    Here's the deal...

    I have built a controller which all controllers extend from, "Controller_Core".
    On it, I verify if the user is authenticated on the system, by validating the Session.

    My problem is, on the before method, I verify if he is logged in, if he's not, I want to show the user the login screen.
    That's all.

    On CodeIgniter I would simply use $this->output->_display(); then an exit or die right after.
    This would display the view I wanted and it would stop there.

    In FuelPHP, I have read that for this kind of logic, I rather use the "router" method.
    But if I use the "router" method, if the logic doesn't stop in the function, nothing is processed after.

    public function router($method, $params)
    {
    if ( !\Auth::perform_check())
    {
    return \Markup::prepare('login')
    ->no_menu()
    ->no_footer()
    ->render();
    }
    }

    The parent (Controller) has no method router, so if I try to call parent::router($method, $params) it just errors me.
  • HarroHarro
    Accepted Answer
    You need the router() method for the same reasons as in CodeIgniter, which is to capture all calls to the controller, for example because there is no one-to-one method mapping.

    The before() method is a prepping method, to do some initialisation work before the requested method is called. It is not designed to alter the flow of the request.

    If your login method is in a different controller, you can get away with a check in before() and if not logged in, redirect to the login controller. If your login method is in the same controller (or in your base controller), you need to use the router() method because you need to call that method instead of the requested method.

    I don't know what this Markup class is doing, but it looks like you're trying to force the framework to do something it is not designed for.

    Every controller method MUST return an instance of Response. All base controllers have an after() method to make sure this happens, in case your methods return something else (like a string or a View object).

    As of 1.6, all included base controllers will have a router() method. For older versions, you have to add the code to the router that calls the original method requested yourself. A quick fix would be to backport the 1.6 base controllers to 1.5, and call parent::router($method, $params).
  • @Harro Verton thank you for you reply.

    It is more clear now what I should be doing based on what I want to do.
    Instead of forcing a output, I used the before method to make a validation, in negative case, I just redirect the request to another controller, like you said.

    The Markup class is a "helper" class I have designed to help me process views.
    Basically because the whole system uses the same HTML Markup, but only varies content/data, I have built a class to do that for me in a easier OOP way.
    Let's just say it's a class to do the process of lazy rendering views {layout {head {metas, stylesheets, head_hs}, header {menu}, content, footer, last-load-content {scripts}}

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion