I would like to check the array given to a view in a controller function has certain key value pairs. How do I do this using phpunit testing?
//my controller I am testing
public function getEdit ($user_id)
{
$this->data['user'] = $user = \Models\User::find($user_id);
$this->data['page_title'] = "Users | Edit";
$this->data['clients'] = $user->account()->firstOrFail()->clients()->lists('name', 'id');
$this->layout->with($this->data);
$this->layout->content = \View::make('user/edit', $this->data);
}
//my test
public function testPostEdit (){
$user = Models\User::find(parent::ACCOUNT_1_USER_1);
$this->be($user);
$response = $this->call('GET', 'user/edit/'.parent::ACCOUNT_1_USER_1);
//clients is an array. I want to get this
//array and use $this->assetArrayContains() or something
$this->assertViewHas('clients');
$this->assertViewHas('content');
}
TL;DR; Try $data = $response->getOriginalContent()->getData();
I found a better way to do it. I wrote a function in the TestCase which returns the array I want from the view data.
protected function getResponseData($response, $key){
$content = $response->getOriginalContent();
$data = $content->getData();
return $data[$key]->all();
}
So to get a value from the $data object I simply use $user = $this->getResponseData($response, 'user');
Inside a test case use:
$data = $this->response->getOriginalContent()->getData();
Example:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testExample()
{
$data = $this->response->getOriginalContent()->getData();
// do your tests on the data
}
}
Example dumping data so you can see what in data(array) passed to view:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testExample()
{
$data = $this->response->getOriginalContent()->getData();
dd($data);
}
}
Should get something back like what's in image:
I managed it by doing it in a messy way. I used assertViewHas:
$this->assertViewHas('clients', array('1' => 'Client 1', '6' => 'Client2'));
So looking at how assertViewHas is implemented HERE it looks like, what the method does, is access the view's data after this call:
$response = $this->client->getResponse()->original;
In your code, the line:
$response = $this->call('GET', 'user/edit/'.parent::ACCOUNT_1_USER_1);
essentially returns the same thing as the line above it, namely a \Illuminate\Http\Response (which extends the symfony component \HttpFoundation\Response)
So, inside the assertViewHas function it looks like laravel accesses the data using $response->$key, so I would try to access the clients and 'content' variables through the $response object.
If that doesn't work try searching around the TestCase file in the Laravel framework ... I'm sure the answer is in there somewhere. Also try to dump the $response object and see what it looks like, there should be some clues there.
The first thing I would try, though, is accessing your data through the $response object.
You can access data in the response and it can be checked..
public function testSimpleLastProducts() {
$res = $this->call('GET', '/');
$this->assertResponseOk();
$this->assertViewHas('lastProducts');
$lastProductOnView = $res->original['lastProducts'];
$this->assertEquals(6, count($lastProductOnView));
}
This worked for me:
$response->getSession()->get("errors")
And from there you can check the contents of the message box for whatever error you might want verify.
Looking for something like this in 2019, I got:
$response->getData()->data[0]->...my properties
Still looking for a simpler way to access it.
I have had the same problem, but my case was a bit special, because I push my data to view via view()->share($params); and in such cases the solution: $content = $response->getOriginalContent()->getData(); does not give the data out.
and I could not use $response->assertViewHas(...) because my data was objects (models) and I needed to verify object properties (keys and id-s).
So my solution was
$data = $response->original->gatherData();
$this->assertSame($currency->key, $data['currency']->key);
Tested on Laravel 8
Related
A lot of pieces to this so here's the meat. Code very slightly tweaked for brevity.
Extended class:
<?php
namespace App\Http;
use Illuminate\Http\Request as LaravelRequest;
class Request extends LaravelRequest
{
}
Middleware:
<?php
namespace App\Http\Middleware;
use App\Http\Request as CustomizedRequest;
use Closure;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
class CustomizeRequest
{
protected $app;
protected $customizedRequest;
public function __construct(Application $app, CustomizedRequest $customizedRequest){
$this->app = $app;
$this->customizedRequest = $customizedRequest;
}
public function handle(Request $request, Closure $next){
$this->app->instance(
'request',
Request::createFrom($request, $this->customizedRequest);
);
return $next($this->customizedRequest);
}
}
Routes:
Route::get('/books1/{id}',[BookController::class, 'frontend1']);
Route::get('/books2/{id}',[BookController::class, 'frontend2']);
Controller:
<?php
namespace App\Http\Controllers;
use App\Models\Book;
class BookController extends Controller
{
public function frontend1(\Illuminate\Http\Request $request){
dump($request);
dump($request->all());
dump($request->route('id'));
return Book::all();
}
public function frontend2(\App\Http\Request $request){
dump($request);
dump($request->all());
dump($request->route('id'));
return Book::all();
}
}
The /books1/5?foo=bar and frontend1() path works. $request is populated as expected.
The /books2/5?foo=bar and frontend2() path is broken. $request has vast amounts of missing data, like it was instantiated with nothing.
Evidently if I type-hint my subclass instead of the more generic parent, it's causing some kind of broken instantiation. From an OO perspective I think this should be perfectly fine and I do specifically need my subclass being provided so prefer that type-hint. Is something deep within Laravel tripping this up? Is this some obscure PHP behavior I haven't seen before?
This is kind of tricky.
First of all, you need to be familiar with the service container and dependency injection. Here is the full doc: https://laravel.com/docs/8.x/container
When you type hint a class inside a controller method, Laravel will try to understand what it should do with it.
If nothing is registered inside the service container, it will try to make a new instance of it.
\Illuminate\Http\Request is bound as a singleton (https://laravel.com/docs/8.x/container#binding-a-singleton).
While a simple bind will return a new instance at each call, a singleton will always return the exact same instance.
Here is a quick demo:
\App\Models\User::class is a class that is not explicitly bound.
When you try to resolve it using the service container, it will not find it and will try to make a new instance:
$u1 = app(\App\Models\User::class);
// Searching \App\Models\User::class...
// Cannot find \App\Models\User::class...
// returning new \App\Models\User();
$u2 = app(\App\Models\User::class);
// same process again
$u3 = app(\App\Models\User::class);
// and again
// You can check these instances are indeed different by checking their hash:
dd(
spl_object_hash($u1), // 000000004af5213500000000220f0bc0 (52135)
spl_object_hash($u2), // 000000004af5213400000000220f0bc0 (52134)
spl_object_hash($u3) // 000000004af5213700000000220f0bc0 (52137)
);
But since \Illuminate\Http\Request::class is bound by Laravel, it follows a different path:
$r1 = app(\Illuminate\Http\Request::class);
// Searching \Illuminate\Http\Request::class...
// Found it! Bound as a singleton.
// returning new \Illuminate\Http\Request() and storing the
// instance in case it is required again later;
$r2 = app(\Illuminate\Http\Request::class);
// Searching \Illuminate\Http\Request::class...
// Found it and already called! Returning the stored instance ($r1)
$r3 = app(\Illuminate\Http\Request::class);
// Searching \Illuminate\Http\Request::class...
// Found it and already called! Returning the stored instance ($r1)
// Their hash are the same
dd(
spl_object_hash($u1), // 0000000011f522cf0000000077704cd1
spl_object_hash($u2), // 0000000011f522cf0000000077704cd1
spl_object_hash($u3) // 0000000011f522cf0000000077704cd1
);
Now, what's happening?
Under the hood, when a new request is made to your app and before hitting the controller method, Laravel will do a lot of things to prepare the \Illuminate\Http\Request instance.
For instance, it will setup the route resolver inside Illuminate\Routing\Router:
/**
* Return the response for the given route.
*
* #param \Illuminate\Http\Request $request
* #param \Illuminate\Routing\Route $route
* #return \Symfony\Component\HttpFoundation\Response
*/
protected function runRoute(Request $request, Route $route)
{
// here
$request->setRouteResolver(function () use ($route) {
return $route;
});
//
$this->events->dispatch(new RouteMatched($route, $request));
return $this->prepareResponse($request,
$this->runRouteWithinStack($route, $request)
);
}
Each time Laravel internally call a method like this:
protected function method(Request $request){
// do something to $request
}
$request is always the same instance, because it is bound as a singleton.
We are now in your controller.
public function frontend1(\Illuminate\Http\Request $request){
// Searching \Illuminate\Http\Request::class...
// Found it and already called!
// Returning the stored instance that has been prepared through all
// Laravel core classes
dump($request);
dump($request->all()); //well prepared
dump($request->route('id')); //well setup
return Book::all();
}
public function frontend2(\App\Http\Request $request){
// Searching \App\Http\Request::class...
// Cannot find \App\Http\Request::class...
// returning new \App\Http\Request();
dump($request);
dump($request->all()); //nothing
dump($request->route('id')); //empty
return Book::all();
}
If you are still here, how to solve this problem?
The easiest way is to use a FormRequest, initially designed to handle form validation, but if you return an empty rules array, you should be able to do everything you did with your custom \App\Http\Request instance:
<?php
namespace App\Http;
use Illuminate\Foundation\Http\FormRequest;
class Request extends FormRequest
{
public function rules()
{
return [];
}
}
Try again, everything should work fine, since this is a feature specially designed to replace the initial \Illuminate\Http\Request object.
The full doc is here: https://laravel.com/docs/8.x/validation#creating-form-requests
My idea is to send https request to all the URLs saved in my database using a model called Notifications.
class guzzleController extends Controller
{
public function guzzle() {
$client = new Client();
$notes=Notification::all();
$response = $client->get($notes);
$response=$response->getStatusCode();
var_dump($response);
}
}
For some reason the get method expects string, and it gave me an error:
InvalidArgumentException in functions.php line 62: URI must be a string or UriInterface
How can I fix this? Anyone with a better idea?
this is actually my notification class
namespace App;
use App\Status;
use App\Notification;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
protected $fillable = ['id','website_url','email','slack_channel','check_frequency','alert_frequency','speed_frequency','active'];
public function statuses(){
return $this->belongsToMany('App\Status')->withPivot('values')->withTimestamps();
}
You're just saying you're using a Client class but there's no trace of use statements here because you're not showing all the code that is needed for us to figure this out. We don't even know what are the get method parameters. My guess is that you're getting an array of Notification class entities back from this statement: $notes=Notification::all();.
So first of all you should be iterating over them and then you call the client on each one of them. But also then you may need to provide just a string to the get method. Can't say how since there's no code about the Notification class as well.
EDIT:
Given the code you provided I think you should try with something like this:
class guzzleController extends Controller
{
public function guzzle()
{
$client = new Client();
$notes = Notification::all();
foreach ($notes as $note) {
$response = $client->get($note->website_url);
$response = $response->getStatusCode();
var_dump($response);
}
}
}
As the error message says, the guzzle client's get() method accepts either a string or a UriInterface implementation. You're fetching the data from Notification model (which returns a Illuminate\Support\Collection not an array of URIs) and feed it directly to the client. You need to prep your data for the client. Something like this:
use Notification;
use GuzzleHttp\Client;
class GuzzleController extends Controller
{
public function guzzle()
{
$client = new Client();
$notes = Notification::all();
// To do this in a more Laravelish manner, see:
// https://laravel.com/docs/5.3/collections#method-each
foreach ($notes as $note) {
// Assuming that each $note has a `website_url` property
// containing the URL you want to fetch.
$response = $client->get($note->website_url);
// Do whatever you want with the $response for this specific note
var_dump($response);
}
}
}
Im having trouble understanding how to access the instance of Slim when a route is in a seperate class than index.php
When using Slim Framework 2 I always used the following, but its not working in Slim 3:
$this->app = \Slim\Slim::getInstance();
Im trying to access a database connection I have setup in the container, but from a separate class. This is what I currently got in my index.php to initiate a Slim app:
require_once("rdb/rdb.php");
$conn = r\connect('localhost');
$container = new \Slim\Container;
$container['rdb'] = function ($c){return $conn;}
$app = new \Slim\App($container);
And here is my route:
$app->get('/test','\mycontroller:test');
And this is what I got in my mycontroller.php class which my route points to, which obviously is not working as $this->app doesn't exist:
class mycontroller{
public function test($request,$response){
$this->app->getContainer()->get('rdb');
}
The error message is the following, due to getinstance not being part of Slim 3 compared to Slim 2:
Call to undefined method Slim\App::getInstance()
Grateful for any help,
Regards
Dan
Have a look at the Slim 3 Skeleton created by Rob Allen.
Slim 3 heavily uses dependency injection, so you might want to use it too.
In your dependencies.php add something like:
$container = $app->getContainer();
$container['rdb'] = function ($c) {
return $conn;
};
$container['Your\Custom\Class'] = function ($c) {
return new \Your\Custom\Class($c['rdb']);
};
And in your Your\Custom\Class.php:
class Class {
private $rdb;
function __construct($rdb) {
$this->rdb = $rdb;
}
public function test($request, $response, $args) {
$this->rdb->doSomething();
}
}
I hope this helps, if you have any more questions feel free to ask.
Update:
When you define your route like this
$app->get('/test', '\mycontroller:test');
Slim looks up \mycontroller:test in your container:
$container['\mycontroller'] = function($c) {
return new \mycontroller($c['rdb']);
}
So when you open www.example.com/test in your browser, Slim automatically creates a new instance of \mycontroller and executes the method test with the arguments $request, $response and $args.
And because you accept the database connection as an argument for the constructor of your mycontroller class, you can use it in the method as well :)
With Slim 3 RC2 and onwards given a route of:
$app->get('/test','MyController:test');
The CallableResolver will look for a key in the DIC called 'MyController' and expect that to return the controller, so you can register with the DIC like this:
// Register controller with DIC
$container = $app->getContainer();
$container['MyController'] = function ($c) {
return new MyController($c->get('rdb'));
}
// Define controller as:
class MyController
{
public function __construct($rdb) {
$this->rdb = $rdb;
}
public function test($request,$response){
// do something with $this->rdb
}
}
Alternatively, if you don't register with the DIC, then the CallableResolver will pass the container to your constructor, so you can just create a controller like this:
class MyController
{
public function __construct($container) {
$this->rdb = $container->get('rdb');
}
public function test($request,$response){
// do something with $this->rdb
}
}
I created the following base controller and extended from that. Only just started playing with Slim but it works if you need access to to the DI in your controllers.
namespace App\Controllers;
use Interop\Container\ContainerInterface;
abstract class Controller
{
protected $ci;
/**
* Controller constructor.
*
* #param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->ci = $container;
}
/**
* #param $name
* #return mixed
*/
public function __get($name)
{
if ($this->ci->has($name)) {
return $this->ci->get($name);
}
}
}
Then in your other controllers you can use it like this.
namespace App\Controllers;
/**
* Class HomeController
*
* #package App\Controllers
*/
class HomeController extends Controller
{
/**
* #param $request
* #param $response
* #param $args
* #return \Slim\Views\Twig
*/
public function index($request, $response, $args)
{
// Render index view
return $this->view->render($response, 'index.twig');
}
}
Important
I upvoted #mgansler and you should read that first if dealing with slim 3, and read this only if interested in differences to slim 2.
Update
So it seems those usages were just old code no one cleaned.
However im leaving this post here as it should be helpful to anyone using Slim 2 (as slim 3 is very much still beta) and as a referance point to help see differences.
Old Update (see above)
Following update of OP, i looked at github source code and found that getInstance is still very much there, but with some slight differences perhaps...
https://github.com/slimphp/Slim/search?utf8=%E2%9C%93&q=getInstance
Test files (which maybe outdated, but unlikely) show something like this:
public function testGetCallableAsStaticMethod()
{
$route = new \Slim\Route('/bar', '\Slim\Slim::getInstance');
$callable = $route->getCallable();
$this->assertEquals('\Slim\Slim::getInstance', $callable);
}
But at the same time we see calls like this in some files, which are obviously contextual and either return diff object ($env) or are in same static file (Slim.php)
$env = \Slim\Environment::getInstance(true);
static::getInstance();
But this does show the static function still exists, so use my examples below and try to figure out why not working for you in current form.
Also, this 'maybe' of interest, as only obvious example of slim3 in usage: https://github.com/akrabat/slim3-skeleton
Though other projects prob exist, search with github filters if still having issues.
Original Answer content
Please include more detail on the route and the other class, but here are 3 ways, with execution examples detailed further down.
This info does relate to Slim Framework 2, not the Slim 3 beta, but slim 3 beta shows similar example code and makes no mention of overhauling changes, and in fact links to the Slim 2 documentation: http://docs.slimframework.com/configuration/names-and-scopes/
$this->app->getContainer()->get('rdb');
// Recommended approach, can be used in any file loaded via route() or include()
$app = \Slim\Slim::getInstance();
Slim::getInstance();
App::config('filename');
Slim3 Beta has only one code example, which looks like this:
$app = new \Slim\App();
// which would by extension mean that this 'might' work too
$app = \Slim\App::getInstance();
// but be sure to try with slim2 naming just in case
$app = \Slim\Slim::getInstance()
Though obviously this doesnt fit outside of index.php, but is consistent with Slim2 doco showing GetInstance works.
Which one fits you?
I have multiple files that use these different approaches, though i cant say what fits best as too little context on how this external class fits in and what its composition is.
For example, my controllers (which are endpoints of most my routes) use the same approach, through a base class or just direct:
class ApiBaseController /// extends \BaseController
{
protected $app;
protected $data;
public function __construct()
{
$this->app = Slim\Slim::getInstance();
$this->data = array();
}
//...
}
class VideoApiController extends \ApiBaseController
{
// ...
public function embed($uid)
{
// trace($this->app->response->headers());
$vid = \R::findOne('videos'," uid = ? ",array($uid));
if(!empty($vid))
{
// embed logic
}else{
// see my baseclass
$this->app->render('api/404.html', array(), 404);
}
}
// ...
// Returns the video file, keeping actual location obscured
function video($uid)
{
require_once(APP_PATH.'helpers/player_helper.php');
$data = \R::findOne('videos'," uid = ? ",array($uid));
/// trace($_SERVER); die();
if($data)
{
stream_file($data['filename']);
}else{
$app = \Slim\Slim::getInstance();
$app->render('404.html');
}
/// NOTE - only same domain for direct /v/:uid call
header('Access-Control-Allow-Origin : '.$_SERVER['HTTP_HOST']);
// header('X-Frame-Options: SAMEORIGIN');
// Exit to be certain nothing else returned
exit();
}
//...
}
My helper files show code like this:
function get_permissions_options_list($context = null)
{
if(empty($context)) $context = 'user';
return App::config('permissions')[$context];
}
My middleware:
function checkAdminRoutePermissions($route)
{
$passed = runAdminRoutePermissionsCheck($route);
if($passed)
return true;
// App::notFound();
// App::halt(403, $route->getPattern());
if(!Sentry::check())
App::unauthorizedNoLogin();
else
App::unauthorized();
return false;
}
Thats example of how i access in the various files, though the code you shared already shows that you have used the recommended approach already
$app = \Slim\Slim::getInstance();
Though again, need more info to say for sure how your external file fits in, but if its at the end of a route or in an 'include()', then it should work.
You said your old approach didnt work though, but gave no info on what the actual result vs expected result was (error msg, ect), so if this doesnt work please update the OP.
This was a tough one. #mgansler answer was really helpful, but in his answer he passed a database connection, and not exactly $app inside the controller
Following the same idea it is possible to send $app though.
First inside your dependencies.php you need to grab the $app and throw it in a container to inject it to the Controller later.
$container['slim'] = function ($c) {
global $app;
return $app;
};
Then you got to inject it:
// Generic Controller
$container['App\Controllers\_Controller'] = function ($c) {
return new _Controller($c->get('slim'));
};
Now on your controller.php:
private $slim;
/**
* #param \Psr\Log\LoggerInterface $logger
* #param \App\DataAccess $dataaccess
* #param \App\$app $slim
*/
public function __construct(LoggerInterface $logger, _DataAccess $dataaccess, $slim)
{
$this->logger = $logger;
$this->dataaccess = $dataaccess;
$this->slim = $slim;
}
Now you just got call it like this:
$this->slim->doSomething();
I am setting up a REST service for my website with the FOSRestBundle and JMSSerializerBundle.
I made a custom method on a entity repository which returns a Paginator object. The method works great when I use it on the normal website, but when I want to use the method with the REST route, this error is thrown (XML or JSON output throws the same error) :
"Resources are not supported in serialized data."
I really don't know where to search since the error isn't very explicit to me.
Here's my AdsRestController.php :
<?php
namespace MyProject\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Controller\Annotations\Get;
class AdsRestController extends Controller
{
/**
* #View
* #Get("/ads/list/all/{page}", requirements={"page" = "\id+"}, defaults={"page" = 1})
*/
public function getAdsListAllAction($page) {
$theAds = $this->getDoctrine()->getRepository('MyProjectMainBundle:Ads')->getAds($page);
return $theAds;
}
}
and my AdsRepository.php :
<?php
namespace MyProject\MainBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
class AdsRepository extends EntityRepository
{
public function getAds($page=1, $maxPerPage=10)
{
$query = $this->createQueryBuilder('a')
->orderBy('a.date', $order)
;
$query->getQuery();
$query
->setFirstResult(($page-1) * $maxPerPage)
->setMaxResults($maxPerPage)
;
return new Paginator($query, true);
}
}
Any help would be highly appreciated !
Thanks.
You can use iterator_to_array to convert iterator of your paginator into array :
return iterator_to_array($theAds->getIterator());
Convert result manually to an array by using getAds()->toArray() in your rest controller.
already answered here, use the search!
Check out the ->getIterator() method available on Paginator objects.
See https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Pagination/Paginator.php
If you use iterator_to_array, it will convert the result array to a single object. It is better to fetch them to an array and then serialize it.
$var = [];
foreach ($records as $rec){
array_push($var, $rec);
}
$res = $this->get('jms_serializer')->serialize($var, 'json');
return new JsonResponse(json_decode($res));
I am very new to symfony. In other languages like java and others I can use request.getParameter('parmeter name') to get the value.
Is there anything similar that we can do with symfony2.
I have seen some examples but none is working for me. Suppose I have a form field with the name username. In the form action I tried to use something like this:
$request = $this->getRequest();
$username= $request->request->get('username');
I have also tried
$username = $request->getParameter('username');
and
$username=$request->request->getParameter('username');
But none of the options is working.However following worked fine:
foreach($request->request->all() as $req){
print_r($req['username']);
}
Where am I doing wrong in using getParameter() method. Any help will be appreciated.
The naming is not all that intuitive:
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request)
{
// $_GET parameters
$request->query->get('name');
// $_POST parameters
$request->request->get('name');
Update Nov 2021: $request->get('name') has been deprecated in 5.4 and will be private as of 6.0. It's usage has been discouraged for quite some time.
I do it even simpler:
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request)
{
$foo = $request->get('foo');
$bar = $request->get('bar');
}
Another option is to introduce your parameters into your action function definition:
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request, $foo, $bar)
{
echo $foo;
echo $bar;
}
which, then assumes that you defined {foo} and {bar} as part of your URL pattern in your routing.yml file:
acme_myurl:
pattern: /acme/news/{foo}/{bar}
defaults: { _controller: AcmeBundle:Default:getnews }
You can Use The following code to get your form field values
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request)
{
// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');
}
Or You can also get all the form values as array by using
$request->request->all()
try
$request->request->get('acme_demobundle_usertype')['username']
inspect attribute name of your formular field
Inside a controller:
$request = $this->getRequest();
$username = $request->get('username');
As now $this->getRequest() method is deprecated you need to inject Request object into your controller action like this:
public function someAction(Request $request)
after that you can use one of the following.
If you want to fetch POST data from request use following:
$request->request->get('var_name');
but if you want to fetch GET data from request use this:
$request->query->get('var_name');
Your options:
Simple:
$request->request->get('param') ($_POST['param']) or
$request->query->get('param') ($_GET['param'])
Good Symfony forms with all validation, value transormation and form rendering with errors and many other features:
http://symfony.com/doc/current/book/forms.html
http://symfony.com/doc/current/cookbook/form/index.html
Something in between (see example below)
<?php
/**
* #Route("/customers", name="customers")
*
* #param Request $request
* #return Response
*/
public function index(Request $request)
{
$optionsResolver = new OptionsResolver();
$optionsResolver->setDefaults([
'email' => '',
'phone' => '',
]);
$filter = $optionsResolver->resolve($request->query->all());
/** #var CustomerRepository $customerRepository */
$customerRepository = $this->getDoctrine()->getRepository('AppBundle:Customer');
/** #var Customer[] $customers */
$customers = $customerRepository->findFilteredCustomers($filter);
return $this->render(':customers:index.html.twig', [
'customers' => $customers,
'filter' => $filter,
]);
}
More about OptionsResolver - http://symfony.com/doc/current/components/options_resolver.html
You can do it this:
$clientName = $request->request->get('appbundle_client')['clientName'];
Sometimes, when the attributes are protected, you can not have access to get the value for the common method of access:
(POST)
$clientName = $request->request->get('clientName');
(GET)
$clientName = $request->query->get('clientName');
(GENERIC)
$clientName = $request->get('clientName');
Most of the cases like getting query string or form parameters are covered in answers above.
When working with raw data, like a raw JSON string in the body that you would like to give as an argument to json_decode(), the method Request::getContent() can be used.
$content = $request->getContent();
Additional useful informations on HTTP requests in Symfony can be found on the HttpFoundation package's documentation.
For symfony 4 users:
$query = $request->query->get('query');
$request = Request::createFromGlobals();
$getParameter = $request->get('getParameter');
use Symfony\Component\HttpFoundation\Request;
public function indexAction(Request $request, $id) {
$post = $request->request->all();
$request->request->get('username');
}
Thanks , you can also use above code
#www.example/register/admin
/**
* #Route("/register/{role}", name="app_register", methods={"GET"})
*/
public function register(Request $request, $role): Response
{
echo $role ;
}
If you need getting the value from a select, you can use:
$form->get('nameSelect')->getClientData();
Try this, it works
$this->request = $this->container->get('request_stack')->getCurrentRequest();
Regards
public function indexAction(Request $request)
{
$data = $request->get('corresponding_arg');
// this also works
$data1 = $request->query->get('corresponding_arg1');
}