I'm trying to use $input->all() as opposed to Input::all() in Laravel-5, however it doesn't seem to like it, even though I am passing the Input reference to the function, like so:
/**
* Search for a specified resource.
*
* #return Response
*/
public function search(Booking $booking, Input $input)
{
dd($input->all()); // this doesn't work
dd(Input::all()); // this DOES work
}
The error I get is:
Call to undefined method Illuminate\Support\Facades\Input::all()
Does anyone have a solution to this problem?
I don't think you're supposed to inject Facades into your Controllers. Input is a facade for Illuminate\Http\Request and it's service container binding is request. So according to the documentation, in Laravel 5 you can do Request::all() and in Laravel 5.1 you can do $request->all()
http://laravel.com/docs/5.0/requests#retrieving-input
http://laravel.com/docs/5.1/requests#retrieving-input
EDIT: This post gives some more in-depth information: https://stackoverflow.com/a/29961400/2433843
EDIT3: I think it would be great if someone could explain WHY exactly you can't inject Facades into your Controllers. I understand DI and Facades are two different things entirely, and L5+ is pushing the developers towards DI. I just don't exactly understand why injecting a facade wouldn't work, since it points towards another class, and it works when you do not inject it. Not to forget Facades and Aliases are two seperate things too. I hope someone can elaborate on this.
One more important thing about using Request or Input to access the User Input is the version of Laravel that you are using.
In the Laravel 4.2 and prior, you could have access the Input::all(), Input::get() but from Laravel 5 onwards, it's been suggested to use the Input via Request facade
Ref: https://laravel.com/docs/5.2/requests
In case if you want to make use of Input in Laravel 5.0 and onwards, then you need to add this facade in the config/app.php file under the aliases section as 'Input' => Illuminate\Support\Facades\Input::class
Once you add the facade under alias, you should start using the 'Input::all()'
Hope this helps some others, who are having the confusion as whether to use 'Input' or 'Request' for Laravel 5.0 onwards.
Related
I am trying to use the Symfony dependency injection component and I was just wondering if anyone would be able to help with the registering of the services.
I have recently moved to ContainerControllerResolver opposed to ControllerResolver so that my routes correctly instantiate the dependencies.
This is working well enough however I have ran into an issue when a class is needed more than once.
So for ContainerControllerResolver to work you seem to have to use the full class path. Where as when you use ControllerResolver you can set your own unique string ID.
I can't see anyway to set a unique ID when using ContainerControllerResolver and you don't seem able to pass arguments to the Reference class.
Is there anyway I could rewrite the below so they are seperate instances? The ControllerResolver way of being able to set unique IDs makes sense to me but I'm just a little lost when you have to pass the full class.
$containerBuilder->register(App\Models\Database::class, App\Models\Database::class)->setArguments([
DB1, DB1USER, DB1PASS
]);
$containerBuilder->register(App\Models\News::class, App\Models\News::class)->setArguments([
new Reference(App\Models\Database::class)
]);
$containerBuilder->register(App\Models\Database::class, App\Models\Database::class)->setArguments([
DB2, DB2USER, DB2PASS
]);
$containerBuilder->register(App\Models\Reports::class, App\Models\Reports::class)->setArguments([
new Reference(App\Models\Database::class)
]);
I think I got the wrong gist of things from another post. They mentioned it is more ideal to use the controller name in the ID when using the ContainerControllerResolver. I've been able to use service IDs in the first param of the register and I've just updated my routes _controller param to go to that same name rather than the class namespace path:
$routes = new RouteCollection();
$routes->add('news', new Route('/news', [
'_controller' => 'controller.news::newsOutput',
]));
I am new to Laravel and I wanted to add error validation messages to view and I wonder what is $errors variable and how it works behind the scenes. According my research the variable is an instance of Illuminate\Support\MessageBag but cannot find any further explanation and details about it. Please can you explain or at least give reference to websites that explain it.
According to the documentation,
Note: The $errors variable is available in every Laravel view where the web middleware is applied. It will simply be an empty instance of ViewErrorBag if no validation errors are present.
Also note that before now, we had to explicitly define it by calling Session::get('errors')->all().
Read this for more info:
https://laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors
Also, look for these files in your project for more insight.
/vendor/laravel/framework/src/Illuminate/Http/RedirectResponse.php - withErrors() method.
/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php - handle() method.
Note: I am using Laravel 5.6 as at the time of writing.
$errors in Laravel comes from validation, to further understand about valiation, kindly refer to documentation https://laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors.
To know how it show on every blade, kindly check your app/Http/Kernel.php, there is middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class and refer to this middleware, you will see these lines:
$this->view->share(
'errors', $request->session()->get('errors') ?: new ViewErrorBag
);
Hope this might help.
I'm trying to allow an user to do something like response()->yaml(['their content']), but I don't understand how I'd go on injecting my YAML method to the response() (ResponseFactory) facade.
Is there any guide that would explain on how to do this? Or maybe a quick description from someone? This is the first time I'm trying to build a package for Laravel and it will also be open source!
I checked out this question, but unfortunately I don't see its use case and I don't think it focuses on adding an additional method which would be called via response().
You can you use Response Macros to achieve your aim.
In the boot method of your AppServiceProvider (or in the package ServiceProvider) add the following:
Response::macro('yaml', function ($content) {
return yaml_whatever($content); //Use your implementaion here
});
Now, you can use return response()->yaml($content);
I need to return default data in all actions in my Symfony project.
For example search form in side bar, viewers counter etc...
So i need to return some default data in all actions
return array(
'form' => $form->createView(),
'short_search' => $shortSearch->createView(),
);
I found Add data to return of all actions in a Symfony controller solution, but it fails when I'm using #Template annotation.
Of course i can call render function from twig, but it seems like it's not fast and good idea.
What component I should override in this case???
The Controllers section of the Symfony Best Practices document advises against using the #Template() annotation, so the easy fix to your problem would be to simply not use #Template().
The reason overriding the base Controller's render method doesn't work is because you're not actually calling it, and neither is the framework. Instead, the SensioFrameworkExtraBundle #Template annotation works by installing an event listener for KernelEvents::VIEW (kernel.view) and (after having used a different event to guess the template name, if necessary), directly uses the templating service to render the response.
In the generic case, what you can do instead is install an event listener on kernel.view with a higher priority, and using the GetResponseForControllerResultEvent event provided to add in your parameters. This event listener might look something like
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$parameters = $event->getControllerResult();
//modify parameters
$event->setControllerResult($parameters);
}
with whatever services necessary to get the additional parameters passed in via dependency injection. You may also want to look at the implementation for #Template's TemplateListener for reference.
The Symfony Cookbook has more information on how to set up event listeners.
In your specific case, you're probably going to be generating your $form and $shortSearch entirely from within that event handler, so at the least, your event handler is going to need at least the form service injected.
In my opinion, this is all largely more trouble than it's worth, and it would be better to just remove the #Template annotation instead. (As a bonus, you'll get a minor performance boost, especially if you disable the annotations entirely, because you won't have the overhead of calling those event listeners on every request.)
Consider this code taken from here.
public function getIndex()
{
$posts = Post::orderBy('id','desc')->paginate(10);
// For Laravel 4.2 use getFactory() instead of getEnvironment() method.
$posts->getEnvironment()->setViewName('pagination::simple');
$this->layout->title = 'Home Page | Laravel 4 Blog';
$this->layout->main = View::make('home')->nest('content','index',compact('posts'));
}
As I understand it, pagination limits the number of rows, so I think paginate(10) means select first ten rows in the database. But I absolutely don't understand this.
// For Laravel 4.2 use getFactory() instead of getEnvironment() method.
$posts->getEnvironment()->setViewName('pagination::simple');
or
$posts->getFactory()->setViewName('pagination::simple');
And everything below. Mainly I don't understand what factory means and how it relates to pagination. I went to the laravel docs on Illuminate\Pagination\Factory and Illuminate\View\View but I can't find the meaning of factory. Can anyone explain the code above?
You are essentially setting how the pagination is output in HTML by selecting a specific paginator view, this allows you to have more than one type in an application or use different to the default.
Using multiple pagination types in the same application
Sometimes, you may want to use different pagination types across your
application. By default, Laravel will use the type specified in your
app/config/view.php file, so you need to override this setting when
you wish to use another type. Here is how to do so.
// This code should be in a controller or a route Closure.
// Let’s use the good old example of a list of blog posts.
$articles = Article::paginate(5);
Paginator::setViewName('pagination::simple');
/*
Alternatively, you could also use this to achieve the same result:
$articles->getEnvironment()->setViewName('pagination::simple');
For those who would like to know what’s happening under the hood, here is a more
detailed explanation:
1. Calling paginate() on an Eloquent model or a query builder will return an
instance of \Illuminate\Pagination\Paginator
2. Then, we need to get the related \Illuminate\Pagination\Environment of this
paginator via the well-named getEnvironment() method.
3. Finally, we can specify the pagination type we need. The default value is
'pagination::slider'.
The pagination types that are available by default are located in the
vendor/laravel/framework/src/Illuminate/Pagination/views directory.
*/
Source: http://laravel-tricks.com/tricks/using-multiple-pagination-types-in-the-same-application