Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Issue with 404 Handling
  • Hi,

    I have an issue with the 404 page.



    If the controller "test" doesn"t exist, i can see my 404 page.

    But if the controller "test" exist, and the action "add" doesn't exist, i have an Warning :

  • HarroHarro
    Accepted Answer
    Something in your code destroys $this->response.

    This is created as a new Response object in the before() method, which runs before the router does. Or does your controller have a before(), and you didn't call parent::before() ?
  • Thanks for your answer,

    But i don't call parent::before() because i don't want to use the controller template stuff. I use Fuel_Theme (and Twig) for this
  • I think i will extends \Controller instead of \Controller_Hybrid


  • Your error message mentions: COREPATH/classes/controller/rest.php @ line 157

    So you're already either using Controller_Rest or Controller_Hybrid. For both, it is required that you call the controllers before method, otherwise your controller will not be setup properly.
  • Hi Harro,

    I've look for extends Controller_Rest (or Controller_Hybrid), i call the before function. But same problem.

    If the controller doesn't exist (ie. "test" on my first post), the class "Controller" throw a new HttpNotFoundException. It's ok.

    But if the controller exist, but not the action, it's doesn't thrown a HttpNotFoundException. It's maybe because in the "Controller_Rest" class you do :

    $this->response->status = $this->no_method_status;
    return;

    In the else of "method_exists()" condition ? (line 151)
  • On an API call it shouldn't throw an HttpNotFoundException, the calling client wouldn't know what to do with it, it expects a REST response.

    On a browser call, when you have extended Controller_Rest it should still return the "no-method" status (as it is designed for REST calls), but if you have extended Controller_Hybrid... Maybe in that case an HttpNotFoundException is in order.

    Just checked the code, and it does. So it works as designed?
  • Yes thanks Harro, i see more the functioning.

    I've found the response of my problem, but i didn't know why it's an issue, i explain :

    I have a base controller named "Controller_Base_Backend". And a module "backend", with a controller named "Controller_Backend" which extends \Controller_Base_Backend

    If i go on "/backend/backend/index" it's ok. But if i go on "/backend/wrongcontroller/index" i've a blank page with no HttpNotFoundException.


    But now, if i rename my controller with "Controller_Test" :
    If i go on "/backend/test/index" it's ok. And if i go on "/backend/wrongcontroller/index" i've my 404 page.

    Strange, no ?


    Note: My \Controller_Base_Backend extends Controller_Hybrid
  • is the HttpNotFoundException caught in your index.php? And does your _404_ route point somewhere?

    You don't have a route defined that would capture that URI?
  • Yes the HttpNotFoundException is caught in index.php

    My _404_ route in the application point on the correct action, and work. No route defined with this URI.

    My routes.php in my application :

    return array(
    '_root_'  => 'home/index',  // The default route
    '_404_'   => 'home/404',    // The main 404 route
            // Frontend route
            '/' => array('home/index', 'name' => 'home'),
            'page-(:id)' => array('page/view', 'name' => 'page_view'),
    );

    Routes.php in my module "backend"

    return array(
    '_root_'  => 'backend/test/index',  // The default route
    );


  • If it's can help you :

    I've do a var_dump() on $this->route in \Request in the function execute() :

    With "Controller_Backend" :


    With "Controller_Test" (or "Controller_Something"), $this->route = null (and throw HttpNotFoundException)

    ---

    A var_dump of static::$routes in the function process of \Router :


    ----

    I've have see in \Router::parse_segments() the fallback for default module controller is [Module]/Controller_[Module], so in my case Backend\Controller_Backend.

    In this case, if we have a controller with the same name of the module, FuelPHP can't throw an HttpNotFoundException if we are in the module URI ? (module name in the first segment)
  • If that is all you have, it will default to the index action, and if that exists, it should call it.

    If you have a method (an additional URI segment), and that doesn't exist, either the controller's router method (<= 1.6.1) or the Request (1.7) should throw an HttpNotFoundException.

    Neither should give you a blank page.
  • EDIT: I've checkout the 1.7/develop branch and it's work now. I was on 1.6/master


    ------

    I'm confuse... Now it's the action which don't throw an HttpNotFoundException...

    I start from zero, no module :



    - "/test" => I see "ok"
    - "/test/404" => I see "Error 404"
    - "/test/wrongaction" => I see blank page.

    I think it's clean, but i don't see where is the problem...
  • I can not reproduce that, when I take a stock 1.7 install, and try to access "/welcome/wrongaction", I get the 404 view (APPPATH/views/welcome/404.php) produced by the action_404...

    Your example can also never work, because "/test" would resolve to action_index(), and you're test controller doesn't have that.

    So you must have some other issue somewhere.
  • Yes sorry for my mistake, replace "action_test()" with "action_index()" in my controller. I've the same issue on 1.6 install. On 1.7/develop it's work fine.
  • So what is the problem now exactly, because you've lost me a bit.

    - It is an issue with 1.6.1. Yes, we know that, that's why it was fixed.
    - There is no issue with 1.7. Yes, because we fixed it.

    Am I correct, or is it different?
  • Yes you are correct. The issue was only on 1.6.1.
    My problem is now fixed with 1.7.

    Thanks
  • Ok. Thanks for confirming that.

Howdy, Stranger!

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

In this Discussion