Namespaces in Php/Laravel - laravel

Suppose some controller like this:
<?php
namespace App\Http\Controller
use Illuminate\Http\Request;
use Validator;
use Auth;
class MemberController extends Controller {
//some code
}
Where do Validator and Auth belong to (so I can see their defintion) and what exactly are they?
I've one more question: here (Laravel 5.6 documentaton) it says
we will use the validate method provided by the
Illuminate\Http\Request object.
and when I check here for more information, there's no validate method!
I'd really appreciate it if anyone can help me with these questions.

Validator and Auth are aliases for the Facades of the same name in Illuminate\Support\Facades. They are a static proxy for an instance of a class. You can read about them in the Laravel docs about Facades.
The aliases for these are configured in config/app.php 'aliases' array.
Laravel 5.6 Docs - Facades
For $request->validate(...):
It is a macro. Macros allow you to add functionality to classes that implement macro functionality at run time.
The Illuminate\Foundation\Providers\FoundationServiceProvider#registerRequestValidation sets this macro on Illuminate\Http\Request to allow for a validate method.
/**
* Register the "validate" macro on the request.
*
* #return void
*/
public function registerRequestValidation()
{
Request::macro('validate', function (array $rules, ...$params) {
return validator()->validate($this->all(), $rules, ...$params);
});
}
For a quick read about macros in Laravel check out my article:
asklagbox blog - Using Macros in Laravel

Related

use $this->authorizeResource in boot() laravel backpack crud package

I need to use policy for crud controller in laravel backpack crud package.
I use :
$this->authorizeResource(Post::class);
and i get this error :
Method
App\Http\Controllers\Admin\PostCrudController::authorizeResource does
not exist.
How should i use Policy (specially resource policy) in laravel backpack crud?
You should be able to do that using Laravel's AuthorizesRequests trait. Generated Backpack CRUD Controllers don't have it by default, since not everybody uses this Laravel feature.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\MonsterRequest as StoreRequest;
use App\Http\Requests\MonsterRequest as UpdateRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class MonsterCrudController extends CrudController
{
use AuthorizesRequests;
public function setup()
{
// ...

Hot to use custom Request class instead NovaRequest (FormRequest) for creating resuorce in Laravel Nova?

I make:
php artisan make:request DiscoverRequest
I want use DiscoverRequest instead default NovaRequest for create new entity for specific resource.
In Laravel Nova exist unified ResourceStoreController for all resources.
public function handle(CreateResourceRequest $request)
Route::post('/{resource}', 'ResourceStoreController#handle');
I want to override Request only for one resource.
How this can be implemented?
Can you elaborate what you mean by "override Request for only one resource"?
Normally you can just type hint the new Request in the method like this:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreItineraryRequest;
class ResourceStoreController extends Controller
{
public function handle(DiscoverRequest $request) {
...
}
}

Can laravel blade service injection can be done from outside view

I want to inject service globally for all application views
can i inject it thorough application service provider boot method ?
What service you want to inject? How will you use it?
An easy way to share variables across all views is to call the share method:
view()->share([
'myService' => app()->make(My\Service::class),
]);
You can call this within your controller or maybe inside a middleware to work across many different controllers, too.
Then, in your views, something like this:
#foreach ($myService->getItems() as $item)
...
#endforeach
Follow this steps:
create service provider: php artisan make:provider UserServiceProvider
Go to
app\providers\UserServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Auth;
class UserServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
// key can be anything
// value what you want
View::share('key', 'value');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
}
}
Than register this service provider inside the config\app.php
App\Providers\UserServiceProvider::class,
Now you can access this key for every views.

Separating methods into many controllers?

I noticed that many of the examples in the Laravel docs seem to have Controllers where the class has only one use/method.
For example, in this part of the doc, they have a UpdatePasswordController class with a single method, update():
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
class UpdatePasswordController extends Controller
{
/**
* Update the password for the user.
*
* #param Request $request
* #return Response
*/
public function update(Request $request)
{
// Validate the new password length...
$request->user()->fill([
'password' => Hash::make($request->newPassword)
])->save();
}
}
Normally, I would put a method called updatePassword() in my UserController class (along with signIn(), signUp(), resetPassword(), etc.), but I'm wondering if it's better to create multiple classes, each with a single action?
Normally class is defined for a single purpose. In laravel , for authentication there is a Illuminate base bundle which is optimized for years.
As an example UpdatePasswordController only responsible for updating password,
AuthController only responsible for authentication.
I prefer you to reserch some MVC best practices

Laravel ResetsPasswords Trait

Locally, I have updated this trait to do some different redirecting after the user submits the getEmail() method to request the reset password link. When pushed to production, my editions aren't there. I'm guessing this is because the ResetsPasswords trait is in the laravel framework which is installed separately from my repository on the server.
If this is the case, what's the best way to change how this ResetsPasswords trait functions. Do I make my own and include that in the repository and just change my controller? Below is the PasswordController.
Thanks!
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
protected $redirectPath = '/main';
/**
* Create a new password controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
Update: So, in the ResetsPasswords trait, I modify the redirect getSendResetLinkEmailSuccessResponse() method. So, do I instead just put that method in my controller (with the same name) and my edited code?
protected function getSendResetLinkEmailSuccessResponse($response)
{
...modified code...
}
You should not be making changes to the Laravel vendor files for the reason you stated.
Instead, you should override any of the trait functions you need to modify in your controller.
So just add the method to your controller with your modified code like so:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller {
use ResetsPasswords;
protected $redirectPath = '/main';
public function __construct() {
$this->middleware('guest');
}
protected function getSendResetLinkEmailSuccessResponse($response) {
// modified code that sends an awesome flash message
}
}
Also, if all you're trying to do is change where the user is redirected to then you don't have to override the functions at all. All you have to do is change the redirectPath property in your controller.

Resources