public function store(Request $request)
{
$request->validate([/** ... */]);
if (Auth::check()) {
// ...
}
return view('foo.bar');
}
over something like:
public function store()
{
request()->validate([/** ... */]);
if (auth()->check()) {
// ...
}
return view('foo.bar');
}
I used to think that the first one is more clear since Facades and/or injected dependencies are explicitly defined as parameters/imports. But lately I've been seeing that using helper functions results to more terse code, which kinda leads to lesser cognitive overhead when scanning a certain codebase.
I generally prefer to use helper functions. In my opinion I find that having lots of facades can make the code harder to read - but it's really just a personal preference.
IIRC helper functions just resolve the classes from the container anyway, so there isn't really much difference between using a helper function or a facade.
I really like to use as much default functions as possible. So I'd generally do in the first way. Also I like to use form validation request because it prevents unnecessary data processing if the form is not valid.
IIRC helper functions just resolve the classes from the container anyway, so there isn't really much difference between using a helper function or a facade.
My only gripe is that it messes up with autocompletion for PHPStorm. I hope there's an alternative way, like methods on the base Controller class so that we can do something like:
public function store()
{
// This is already an option
$this->validate();
// Maps to auth()->check(), for example.
if ($this->user()->isAuthenticated()) {
// ...
}
// Akin to how Symfony does it.
return $this->render('foo.bar');
}
@MARTINBEAN - Oh yeah, that's exactly what I'm thinking of! With the trajectory of PHP becoming more Java-like, using global functions just feels out of place. I wouldn't have any issue with it if these functions need to be explicitly imported, though.