Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 / Lumen Request Header?

So I am not really sure how to go about this I have tried a few things and I will list one below however what I am trying to do is store information sent in a http request in a PHP variable.

Here is a view from Chrome Postman of me sending the request I want ot send. Note "pubapi" is a "header".

PostMan View

I have been messing around with Lumen requests as you can see documented here ( http://lumen.laravel.com/docs/requests ) and have tried using the following below to possibly display them but its not working obviously.

echo Request::all();

I am putting this in my controller and I have ...

use Illuminate\Http\Request;

in my controller.

So how could I say store the header I am sending "pubapi" into a php variable in my controller?

EDIT

Not sure if this will help, however looking at the Laravel frameworks docs I see this http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_header trying this throws the same error in my code. So for example I tried the following and reached the same error.

echo Request::header('pubapi');
like image 604
kevingilbert100 Avatar asked Apr 27 '15 21:04

kevingilbert100


4 Answers

You misunderstand the Laravel request object on two levels.

First, the error you are getting is because you were referencing the object instead of the Facade. Facades have a way of forwarding static method calls to non-static methods.

Second, you are sending the value as a header but are trying to access the request parameters. This will never give you what you want.

Here is a simple way to see an example of what you want by creating a test route like so:

Route::match(['get','post'], '/test', function (Illuminate\Http\Request $request) {
    dd($request->headers->all());
});

Post to this route and you will see your headers, one of which will be pubapi. Pay attention that the route method definition matches how you are submitting the request (ie GET or POST).

Let's apply this to the controller, ArticleController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller
{
    public function index(Request $request)
    {
        $pubapi = $request->header('pubapi'); // string
        $headers = $request->headers->all(); // array
        /*
          $pubapi === $headers['pubapi']
        */
    }
}
like image 123
Qevo Avatar answered Oct 03 '22 09:10

Qevo


Try to change the Illuminate\Http\Request to Request.

- use Illuminate\Http\Request;
+ use Request;
like image 35
balintant Avatar answered Oct 03 '22 09:10

balintant


Using

echo app('request')->header('pubapi');

Instead of

echo Request::header('pubapi');

Seemed to work perfect. Could someone provide additional explanation to why this worked and my original method didn't?

like image 40
kevingilbert100 Avatar answered Oct 03 '22 10:10

kevingilbert100


Actually you are calling it statically, that's why it is not getting appropriate Request class and throwing error, can do as follows

use Illuminate\Http\Request;

//inside your controller
class YourClass extends Controller{
   public function yourFunction(Request $request){
        //for getting all the request
        dd($request->all());

        //for getting header content
        dd($request->header('pubapi'));
   }
}
like image 43
Shahrukh Anwar Avatar answered Oct 03 '22 11:10

Shahrukh Anwar