Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error + looking for Examples of how to implement a slightly more complicated auth #204

Closed
iannazzi opened this issue Mar 15, 2016 · 20 comments
Labels

Comments

@iannazzi
Copy link

First off, awesome work on this project!

I have two additional auth fields: 'company' and 'active' that I need to check... I was looking through docs and couldn't find a solution/explanation to help me get passed an error.

I am erring between the route and controller, and looking at the error message I am not finding where to go next.

$api->group([], function ($api) {

//dd('here') //got it.....data coming in ok
$api->post('users/login', 'LoginController@login');

});

class LoginController extends Controller
{
public function login(Request $request)
{
dd('here'); //not getting here....
$credentials = $request->only('company', 'username', 'password');
dd($credentials);

with the json message/error being returned:

{"message":"405 Method Not Allowed","status_code":405,"debug":{"line":8468,"file":"/Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php","class":"Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException","trace":["#0 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(8464): Illuminate\Routing\RouteCollection->methodNotAllowed(Array)","#1 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(8442): Illuminate\Routing\RouteCollection->getRouteForMethods(Object(Dingo\Api\Http\Request), Array)","#2 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(7809): Illuminate\Routing\RouteCollection->match(Object(Dingo\Api\Http\Request))","#3 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(7757): Illuminate\Routing\Router->findRoute(Object(Dingo\Api\Http\Request))","#4 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(7752): Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\Request))","#5 /Users/embrasse-moi/Code/anglar/vendor/dingo/api/src/Routing/Adapter/Laravel.php(65): Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))","#6 /Users/embrasse-moi/Code/anglar/vendor/dingo/api/src/Routing/Router.php(558): Dingo\Api\Routing\Adapter\Laravel->dispatch(Object(Dingo\Api\Http\Request), 'v1')","#7 /Users/embrasse-moi/Code/anglar/vendor/dingo/api/src/Http/Middleware/Request.php(112): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))","#8 [internal function]: Dingo\Api\Http\Middleware\Request->Dingo\Api\Http\Middleware{closure}(Object(Dingo\Api\Http\Request))","#9 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(9487): call_user_func(Object(Closure), Object(Dingo\Api\Http\Request))","#10 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(3214): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Dingo\Api\Http\Request))","#11 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#12 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(9479): call_user_func_array(Array, Array)","#13 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Dingo\Api\Http\Request))","#14 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(9469): call_user_func(Object(Closure), Object(Dingo\Api\Http\Request))","#15 /Users/embrasse-moi/Code/anglar/vendor/dingo/api/src/Http/Middleware/Request.php(113): Illuminate\Pipeline\Pipeline->then(Object(Closure))","#16 /Users/embrasse-moi/Code/anglar/vendor/dingo/api/src/Http/Middleware/Request.php(89): Dingo\Api\Http\Middleware\Request->sendRequestThroughRouter(Object(Dingo\Api\Http\Request))","#17 [internal function]: Dingo\Api\Http\Middleware\Request->handle(Object(Illuminate\Http\Request), Object(Closure))","#18 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(9479): call_user_func_array(Array, Array)","#19 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request))","#20 /Users/embrasse-moi/Code/anglar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))","#21 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing{closure}(Object(Illuminate\Http\Request))","#22 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(9469): call_user_func(Object(Closure), Object(Illuminate\Http\Request))","#23 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(2328): Illuminate\Pipeline\Pipeline->then(Object(Closure))","#24 /Users/embrasse-moi/Code/anglar/bootstrap/cache/compiled.php(2312): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))","#25 /Users/embrasse-moi/Code/anglar/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))","#26 /Users/embrasse-moi/Code/anglar/server.php(19): require_once('/Users/embrasse...')","#27 {main}"]}}

@iannazzi
Copy link
Author

sorry, looks like I am receiving this error even on the posts example, let me double check your tutorial

@iannazzi
Copy link
Author

For more info - my initial post test is passing, So I would guess I am good to get some data in from the forms.

@jadjoubran
Copy link
Owner

Hi @iannazzi
Thank you 😄
The error 405 Method Not Allowed usually means that you're not using the correct verb..
Are you using get instead of post?

@iannazzi
Copy link
Author

hi @jadjourban, I am using post. I am currently writing a test using get to see what is going on there. Next Ill move on to postman. Its weird that the phpunit test i wrote passes...

@iannazzi
Copy link
Author

ohh, I changed the error. I don't think I can use dd('message') to debug... it looks like dingo/api wants me to return a more specific response.... like return $this->response->noContent();

@jadjoubran
Copy link
Owner

hmm keep in mind you can also use ->post('/api/, $data)->dump() to see what was returned from the API

so does it work now?

@iannazzi
Copy link
Author

I am verifying the posted dated using this:

return response()->success($request->all());
I am not sure on which object attach->post('/api/, $data)->dump() ... it looks like I should stick it on $api, but I am not sure where $data is coming in from?

@jadjoubran
Copy link
Owner

You can use the ->dump() in your tests after calling the post or get method to get the output of the API call

@tahirQaiser
Copy link

It shows "{"message":"405 Method Not Allowed","status_code":405,"debug":" to me,
I am using POST Method at

$api->post('users/{user_id}/roles/{role_name}','App\Http\Controllers\HomeController@attachUserRole');

@tahirQaiser
Copy link

same here

@jadjoubran
Copy link
Owner

Hi @rajatahirqaiser
Please show a screenshot of postman or a code snippet of your test

@tahirQaiser
Copy link

@jadjoubran , have a Look

9652983

@tahirQaiser
Copy link

And Here is my Code

public function attachUserRole($UserId,$Role){

        $user = User::find($UserId);

      $RoleId=Role::where('name',$Role)->firstOrFail();;

       $user->roles()->attach($RoleId);

        return $user;
    }

@jadjoubran
Copy link
Owner

@rajatahirqaiser Can you please post the following:

  • route definition (the line that points to this method)
  • how are you calling this? with a sample code

@jadjoubran jadjoubran reopened this Mar 29, 2016
@iannazzi
Copy link
Author

Instead of return $user try using
return response()->success(compact('user'));

@tahirQaiser
Copy link

@iannazzi , It still shows the same ..

@jadjoubran

Here you go :

public function attachUserRole($UserId,$Role){

        $user = User::find($UserId);

    $RoleId=Role::where('name',$Role)->first();

       $user->roles()->attach($RoleId);

        return response()->success(compact('user'));
    }

http://www.example.com/api/users/1/roles/owner

@jadjoubran
Copy link
Owner

@rajatahirqaiser when you visit the URL in your browser, it means you're performing a GET request..
whereas you expect it to be a POST
You can use a tool such as postman to perform POST requests

@jadjoubran
Copy link
Owner

@rajatahirqaiser any updates on this?

@tahirQaiser
Copy link

tahirQaiser commented Jul 26, 2016

@jadjoubran Sometimes, when I try to scrap pages it says failed to open stream HTTP request failed 404 Not found EVEN URL IS WORKING AND TRUE .. sometimes it says URL (503 - Service Unavilable ), How to fix i t

@jadjoubran
Copy link
Owner

Hi @rajatahirqaiser
Can you please open a new issue with more information?
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants