Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Funny $this->response output
  • Hi,

    This is my first time posting on the forums. I am liking it so far, seems like a suped up version of code igniter, which i loved, except that fuelphp is fully namespaced and more with php 5.4 etc

    So I am designing an api for an html5 mobile with angular js.

    Right now I am writing the authentication system, and i am getting funny output.

    I am designing it with a proxy controller, that I then go and send the request to the different places it will need to go. I am using modules for this.

    This is my app/classes/controller/proxy.php



    class Controller_Proxy extends Controller_Rest{
    protected $format='json';
    protected $user_controller;
    protected $user_method;
    protected $user_route;
    public function action_proxy(){
    // get the user called method
    $this->get_call_method();
    if(Session::get('auth') !== NULL){
    if(Session::get('auth')==true){
    // fire off the users requested method
    $result=Request::forge($this->user_route, false)->execute()->response();
    }else{
    $result=Request::forge('auth/auth', false)->execute()->response();
    }
    }else{
    $result=Request::forge('auth/auth', false)->execute()->response();
    }
    $this->response($result->body());


    }
    }


    this is app/modules/auth/classes/controller/auth.php

    namespace Auth;
    class Controller_Auth extends \Controller_Rest{
    protected $format='json';
    public function action_index(){
    return $this->auth();
    }

    // function to authenticate the user and validate login
    public function auth(){
    switch(\Input::method()){
    case 'POST':
    $user=\Input::json('username');
    $pass=\Input::json('password');
    break;
    case 'GET':
    $user=\Input::get('username');
    $pass=\Input::get('password');
    break;
    default:
    break;
    }
    return Model_Auth::validateUser($user, $pass);
    }
    }


    and this is my model for the module in app/modules/auth/classes/model/auth.php

    namespace Auth;
    class Model_Auth extends \Model{

    public static function validateUser($username, $password){
    $query= \DB::query("SELECT id FROM Users WHERE Username='{$username}' AND Password=sha1('{$password}')")->execute();
    $user=$query->as_array();
    if(sizeof($user)>0){
    if(sizeof($user)>1){
    return 'There Seems To Be Multiple Users With Selected Username.';
    }else{
    return $user;
    }
    }else{
    return 'No User With Selected Username.';
    }
    }
    }


    Then in core/classes/response.php

    	/**
    * @var array An array of HTTP headers
    */
    public $headers = array(
    'Access-Control-Allow-Origin' => '*',
    'Access-Control-Allow-Headers' => 'content-type',
    'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS',
    'Access-Control-Max-Age' => '28800'
    );


    The reason I have hardcoded these headers, is that this particular app will be use only for the mobile api, and therefore these headers will always be required, and without them I cannot make the ajax request due to CORS issues.

    So the problem I am having is this:

    When I make the request through the browser, I get this response:

    "{\"userid\":\"1\"}"


    First of all, why is it escpaing the double qoutes?

    And second, when I call it through ajax in the actual application, I get a 500 Internal Server Error.


    So my question is first, why am I not getting a normal json response?
    And second, why does it work from the browser, and not the ajax request?



    Thanks in advance,
    Zedd
  • All I can say is that I think there's something wrong with your server installation.

    If I create a RESTful method here with
    return array('user_id' => 1);

    I get the expected output in the browser
    {"user_id":1}
    without encoded quotes.

    As for the 500, FuelPHP doesn't produce a 500 error out of the box. And if it did, it would look like a FuelPHP exception page. I guess this error is generated by your webserver.
  • any suggestions on what server configurations can fix it?

    I have verified that magic qoutes are off and trying to think of other reasons the output would be returning with slashes..

    Edit: I solved this! Finally!

    Basically my problem was, that I was using a rest controller for my modules controller, and therefore it was returning the data as json to my proxy controller, which was of course also a rest controller, and therefore it was returning the data like that, because it was effectively json_encoding a json_encoded string, double json encoding it..

    And as for my 500 internal server error, it was because I was only doing $this->resonse() and not return $this->response()

    So basically I solved my issues but extending a normal controller instead of a rest controller in my modules controller, and by returning the actual response in my proxy controller
  • Not really, other then to get your debug pants on... sorry.
  • Turns out it was because I was returning the response FROM a rest controller TO a rest controller. I returned it from the second controller normally, without the $this->response() and then in the in first controller I returned it with the $this->response().

    Fixed the issue.

Howdy, Stranger!

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

In this Discussion