Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Question about REST controller
  • Hi,

    I have a REST controller like this:

    <?php
    abstract class Controller_Admin_Api extends \Controller_Rest
    {
        /**
         * Before
         *
         *
         * @return void See Controller_Rest::before()
         */
        public function before()
        {
            // Call parent before function
            parent::before();

            // Logged in?
            if ( ! \Input::is_ajax() or ! isLoggedIn())
            {
                return $this->response(
                    array('status' => 'Not allowed'),
                    400
                );
            }
        }

        public function after($response)
        {
            return parent::after($response);
        }
    }

    and a this class extends the above class:

    <?php
    class Controller_Admin_Api_Test extends \Controller_Admin_Api
    {
        /**
         * Before
         *
         *
         * @return void See Controller_Rest::before()
         */
        public function before()
        {
            // Call parent before function
            parent::before();
        }

        /**
         * Delete a record THIS IS EXAMPLE CODE
         *
         *
         *
         * @return void
         */
        public function get_delete_record($id = null)
        {
            $id = (int) $id;

            \Model_Test::delete($id);
        }
    }

    Ok so if i call the api-url in my browser I see that the header-status-code is 400, but the delete_record function is still being called... Where did I go wrong?
  • HarroHarro
    Accepted Answer
    A before() method is a prepping method, it does not return return values (as is obvious by your parent call that doesn't return a result either).

    If you need to break out of the request flow, you need to use the router() method.
  • aah cool... didn't know that ;-)
  • why is it returning html?

    The requested REST method returned an array or object:<br /><br />{
    "status": "Not Authorized"
    }
  • Ok I set $this->format = 'json'; and it works... thanks
  • Because your request didn't specify you wanted json returned. The best way to do that is to add "application/json" to the http accept-header.

    In general, you don't want the code to force a format, you want the client to specify which format it understands.
  • Is there no Router() method in a normal Controller? Is this method only available in Controller_Rest and Controller_Hybrid?
  • No, this is available in every controller type. But it is optional in all cases.
  • Are you sure? Because I extended a normal \Controller and it could not call the parent router method...
  • There is no parent router() method, the base controller doesn't have one. It would only introduce overhead if it did, since it requires a call_user_func_array() call to continue with an anction.

    The REST base controller does, since it does the format and authentication for all methods called in the router.

    This doesnt mean you can't add a router method to your controller, if you do the Request will automatically call it instead of the action.

Howdy, Stranger!

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

In this Discussion