Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
error 405 on Rest controller
  • Hi,

    quick intro: I used Kohana for a big and full rewrite, then stumbled upon Fuelphp and (re)started from scratch because of the many features built-in and the clear structure. I was then tempted by Laravel (simply because I tried to anticipate the switching cost of moving to Fuel 2.0), but I'm back to Fuel, full speed: this framework is that awesome :)

    I'm using V1.4 on PHP 5.4. Web servers are php built-in and/or nginx 1.2.1
    I've setup a rest controller and the proper route for it, but with both web servers, I get the following error:
    GET 405 Method Not Allowed

    Here is the controller:

    class Controller_Ajax_Index extends Controller_Rest {
    public function get_view() {
    $this->response(array('test'=>'ok'));
    }
    }

    Same result with a index_view() function.

    And the HttpRequest:

    $.ajax({
    type: 'GET',
    url: targetUrl, // /ajax/categories/view/50f04df7c51566a912000008
    success:function(data){
    $('#right-panel').text(data);
    }
    });

    targetUrl is on the same domain, no cross-domain request.
    Also tried with url /ajax/view.json?type=categories&id=50f04df7c51566a912000008
    Any idea?
  • ssianggessiangge
    Accepted Answer
    How do you setup the route? Can you show the content of route.php?

    By default /ajax/categories/view/... will go to:
    Controller_Ajax_Categories->get_view()

  • I've kept on modifying the routes, couldn't figure why it didn't work.

    Turns out Fuel doesn't like a controller named ajax!

    When I changed "ajax" for "rest" in the route, controller and url, everything worked as expected.

    Here is the route:
    'admin/ajax/:type(/:action(/:id)?)?' => 'backend/rest/$3/$4',

    Edge case maybe?
  • 'admin/ajax/:type(/:action(/:id)?)?' => 'backend/rest/$3/$4'

    $1 => :type
    $2 => :action(/:id)
    $3 => :action
    $4 => :id

    This will become "backend/rest/:action/:id". So it will route to:
    Controller_Backend_Rest->get_{:action}

    Is this what you want?


    You mentioned function index_view()? why?

  • Because I kept on getting error 405 with my get_view() function, I've also tried index_view() just to check if the issue was with the "get_" prefixe.

    As I said, using "ajax" as a the controller name seems to be the issue: everything work as expected if change it to something else.

    Thanks for your help.
  • Can't think of any reason why you wouldn't be able to use the name "ajax" for a controller.

    Did you get the same error when you requested the URL through the browser?

  • Tried again this morning with "ajax" and it worked. Weird thing is I had the same behaviour with browser request.

    Must have missed something, probably the route.


  • do you have many routes for 'ajax' ? the sequence may affect which rule will be called first.

  • I commented all the over routes.

    I'm pretty sure the route I was using was sending an incorrect action, sorry for all the fuss :(
  • If you don't mind, you can explain what you want and see who can help you on the route rules.
  • ssiange: problem is solved.

    I was just trying to setup a rest controller for a GET request.
    I did it numerous time, but in this case something went wrong while using a controller name "ajax".
    I though "ajax" may be a reserved name somewhere in the core code, but it was a routing problem; I was sending an incorrect action, hence the "Method not allowed" error.

    Thx

Howdy, Stranger!

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

In this Discussion