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".
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');
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']
*/
}
}
Try to change the Illuminate\Http\Request to Request.
- use Illuminate\Http\Request;
+ use Request;
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?
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'));
}
}
You can do it by request()->header('pubapi') also.
I prefer to use request() helper function than Request class its self. Because it can be used without use/import any class in the controller.
Related
I have this implemented in my PagesControllerTest
<?php
namespace App\Test\TestCase\Controller;
use App\Controller\PagesController;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
class PagesControllerTest extends TestCase
{
use IntegrationTestTrait;
/*public $fixtures = [
'app.Pages'
];*/
public function setUp(){
parent::setUp();
}
public function testIndex()
{
$this->assertTemplate("default");
}
}
already if I run the phpunit tests I get the error "Failed asserting that 'default' equals template file ."
I hope anyone can tell me why the assertTemplate function already returns empty. I think I missed some initialisation but I don't know. The layout in the pagesController is actually disabled but if I comment it out and use default, the result remains same. Nothing.
Your test doesn't actually issue a request, please re-read the docs closely, you need to use the get()/post()/etc methods to simulate a request, quote:
[...] Before you can do any assertions you’ll need to dispatch a request. You can use one of the following methods to send a request:
get() Sends a GET request.
post() Sends a POST request.
put() Sends a PUT request.
delete() Sends a DELETE request.
patch() Sends a PATCH request.
options() Sends an OPTIONS request.
head() Sends a HEAD request.
So something like this:
$this->get('/pages/index');
And after that you can use assertions accordingly.
See Cookbook > Testing > Controller Integration Testing
I keep getting an error that the action is not defined in my controller, but it is. I can access the index action, but not the processOrder action.
Below is my controller and my routes file.
namespace App\Http\Controllers\ThirdPartyAPI;
use App\Order;
use App\ThirdPartyAPI;
use GuzzleHttp\Client;
use App\Jobs\ThirdParyOrders;
use App\ThirdParty\ThirdPartyAPI;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class OrdersController extends Controller
{
public function index ()
{
// list orders
}
public function processOrder()
{
// some logic here
}
}
If my I call the action "#index" in my routes/web.php file, it works and it returns the url, but if I change the "#index" to "#processOrder", it throws the error.
Ie. this works:
Route::get('thirdparty/process-order', function() {
return action('ThirdPartyApi\OrdersController#index');
});
But this doesn't:
Route::get('thirdparty/process-order', function() {
return action('ThirdPartyApi\OrdersController#processOrder');
});
I'm not sure where I'm missing the plot.
I've tried to quit and then re-run:
php artisan serve
I've also tried
composer dump-autoload
Still not sure what the problem was initially, but I've managed to get it working by using a different method.
Intead of using a closure, I do it like this:
Route::get('thirdparty/{thirdparty_client}/process-order/{order}', 'ThirdPartyApi\OrdersController#processOrder');
This seems to do the trick. I didn't know that I could pass multiple parameters to the controller in this way, but this is working 100%.
I think you should have to try this as route.
Route::any("thirdparty/process-order", "ThirdPartyApi\OrdersController#processOrder");
I am developing app in laravel (REST server), using Basic Auth. Using Postman, all GET requests I have implemented seem to work, but unfortunately POST requests not.
routes.php:
Route::post('my/action', 'MyController#postMyAction');
My Controller:
public function __construct()
{
$this->middleware('auth.basic.once');
}
public function postMyAction($request)
{
// some logic here
}
The problem is, that this way, after setting credentials and some params in Postman, following exception appears:
Missing argument 1 for
App\Http\Controllers\MyController::postMyAction()
Does anybody knows how to put request into post-processing function defined in routes?
Thanks in advance.
Laravel provides dependency injection for controller methods, however you need to typehint exactly what you want so Laravel knows what to inject:
public function postMyAction(\Illuminate\Http\Request $request)
{
// Now $request is available
Now Laravel knows you want an instance of Illuminate\Http\Request and it will give it to you.
Of course you can also stick use Illuminate\Http\Request; at the top of your controller then just typehint Request $request as the argument.
Why am I getting this error. I created a PortfolioController. Then I made a route using this
Route::get('portfolio','PortfolioController');
So in my controller page I made this.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PortfolioController extends Controller
{
//This only gets exectued when we request /portfolio/Paintings using GET
public function getPaintings()
{
return 'This RESTful controller is working!';
}
}
I get this error when typing in localhost/portfolio/paintings
From the look of your code, it looks like you're trying to setup an implicit controller route. You're close, but your route definition is a little off. You need to use controller instead of get:
Route::controller('portfolio','PortfolioController');
https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0
The following features are deprecated in 5.2 and will be removed in the 5.3 release in June 2016:
Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file. This will likely be extracted into a package.
You must declare each endpoint now.
try this
Route::get('portfolio','PortfolioController#getPaintings')
I got a similar error when there was a mistake in he file of web.php.
A correct route would like this Route::get('portfolio','YourController#yourMethod');
Use this code in routes:
Route::resource('portfolio','YourController#yourMethod');
you need to explain your function on Route.
example:
Route::methods('your-uri','YourController#YourFunction');
so you should do this:
Route::get('portfolio','PortfolioController#getPaintings');
Hope it helps
You have to consume a function of the controller instead of using the whole controller class for one request. so laravel doesn't know which of your function to use.
Try using PortfolioController#index. or Route::resource('yourroute','PortfolioController');
Try this: Route::resource('/portfolio','PortfolioController');
Hope this will work
I have this following Controllers on my file
class MyController1 extends BaseController{
public function notify($id,$message){
//Sending a push notification
//Google Cloud Messaging Here
//****//
}
}
How do i write the route to pass a value to id and message using GET?
Route::get("/sendMessage" , MyController1#notify);
the url should be something like
https://mysite.com/sendMessage?id=1&message=Hello
Also I need to call the notify method from other controllers like this. .
class MyController2 extends BaseController{
public function something(){
$con = new MyController2();
$con->notify($id,$message);
}
}
What should I put to the notify the model?
This is the code you need to create GET parameters in your URL:
Route::get('sendMessage/{id}/{msg}'
More information here:
http://laravel.com/docs/routing#route-parameters
Greetings
Laravel URL's work slightly differently to how you want it.
In Laravel as standard your URL for the above example will be
https://mysite.com/sendMessage/1/Hello
and your route would be
Route::get("/sendMessage/{id}/{message}" , MyController1#notify);
the text in the braces {} will be the name of the parameter passed to the controller function
eg. https://mysite.com/sendMessage/1/Hello would call MyController->notify(1,'Hello');