N'ikwu ya n'ụzọ ọzọ, ndị na-enye ọrụ dị ka olulu nke anyị na-esi na ya wụsa mmanụ "klas" n'ime tank nke a na-akpọ "akpa ọrụ" nke injin a na-akpọ Laravel.
Ọ bụrụ na anyị emepee config/app.php anyị ga-ahụ n'usoro nwere aha "onye na-eweta"
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
.
.
.
],
Ndị a bụ ụfọdụ ndị na-enye ọrụ na-enye ọnụ na laravel, ya bụ ọrụ ndị bụ isi nke etinyere n'ime akpa ọrụ.
service provider
a na-eme ha?Ọ bụrụ na anyị na-ele akwụkwọ na arịrịọ lifecycle , a na-eme faịlụ ndị a na mbido:
public/index.php
bootstrap/app.php
app/Http/Kernel.php
na nke ya Middlewares
Service Providers
: ọdịnaya nke isiokwu aKedu service provider
eboro ha?
Ha bụ ndị ahụ definites n'usoro config/app.php
:
return [
// ... other configuration values
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
// ... other framework providers from /vendor
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* PUBLIC Service Providers - the ones we mentioned above
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
],
];
Dị ka anyị nwere ike ịhụ, e nwere ndepụta nke service provider
ọ bụghị ọha na nchekwa /vendor
, anyị ekwesịghị imetụ ha aka ma ọ bụ gbanwee ha. Ndị na-amasị anyị bụ n'okpuru, na BroadcastServicerProvider
nwere nkwarụ site na ndabara, ikekwe n'ihi na anaghị adịkarị eji ya.
Ndị na-enye ọrụ ndị a niile na-agba ọsọ site n'elu ruo na ala, na-emegharị ndepụta ahụ ugboro abụọ:
register()
, bara uru maka (n'ikpeazụ) ime ihe ahaziri tupu usoro ahụ boot()
.boot()
nke ndị na-enye ọrụ niile. Ọzọ, otu otu, n'elu ruo ala, nke n'usoro 'providers'
.I Service Providers
gụnyere na Laravel, ndị niile nọ na nchekwa app/Providers
:
AppServiceProvider
AuthServiceProvider
BroadcastServiceProvider
EventServiceProvider
RouteServiceProvider
Ha niile bụ klaasị PHP, nke ọ bụla metụtara isiokwu nke ya: App
, Auth
, Broadcasting
, Events
e Routes
. Mana ha niile nwere otu ihe jikọrọ: usoro boot()
.
N'ime usoro ahụ, anyị nwere ike ide koodu ọ bụla metụtara nke ọ bụla n'ime ngalaba ndị ahụ: auth
, events
, route
, wdg. N'ikwu ya n'ụzọ ọzọ, Ndị na-enye ọrụ bụ klaasị iji debanye aha ụfọdụ ọrụ zuru ụwa ọnụ.
Ha dị iche iche dị ka "ndị na-eweta" n'ihi na ha na-agba ọsọ n'isi mmalite nke usoro ndụ ngwa, yabụ ihe zuru ụwa ọnụ na-adaba adaba ebe a tupu edemede eme ihe erute na Ụdị ma ọ bụ ndị nchịkwa.
Ọtụtụ ọrụ dị na RouteServiceProvider, ebe a bụ koodu:
class RouteServiceProvider extends ServiceProvider
{
public const HOME = '/dashboard';
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->group(base_path('routes/web.php'));
});
}
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
}
}
Nke a bụ klaasị ebe a na-ahazi faịlụ ndị ahụ route
na routes/web.php
e routes/api.php
gụnyere na ndabaradefinita. Mara na maka API enwekwara nhazi dị iche iche: prefix Endpoint /api
na middleware api
maka mmadụ niile routes
.
Anyị nwere ike dezie service providers
, nke na-adịghị na nchekwa /vendor
. A na-emezi faịlụ ndị a mgbe ị nwere ọtụtụ ụzọ ma chọọ ikewa ha na faịlụ ndị akọwapụtara. Ị na-emepụta routes/auth.php
ma tinye ụzọ n'ebe ahụ, mgbe ahụ ị "na-enyere" faịlụ ahụ na usoro boot()
di RouteServiceProvider
, tinye naanị ahịrịokwu nke atọ:
`Route::middleware('web') // or maybe you want another middleware?
->group(base_path('routes/auth.php'));
AppServiceProvider
ọ tọ dụdu. Otu ihe atụ nke ịgbakwunye koodu AppServiceProvider
, bụ maka gbanyụọ umengwụ loading na Eloquent. Iji mee nke a, naanị ị chọrọ tinye ahịrị abụọ n'usoro boot()
:
// app/Providers/AppServiceProvider.php
use Illuminate\Database\Eloquent\Model;
public function boot()
{
Model::preventLazyLoading(! $this->app->isProduction());
}
Nke a ga-atụfu mwepu ma ọ bụrụ na anaghị eburu ụdị mmekọrịta.
service provider
ahaziriNa mgbakwunye na faịlụ ndị mbụdefinites, anyị nwere ike mfe ịmepụta ọhụrụ Service Provider
, metụtara isiokwu ndị ọzọ karịa ndị mbụdefiemechara dị ka auth
/event
/routes
.
Otu ezigbo ihe atụ bụ nhazi nlele Blade
. Anyị nwere ike ịmepụta ntuziaka Blade
, wee tinye koodu ahụ n'ime usoro ahụ boot(
) nke ọ bụla service provider
, gụnyere ndabara AppServiceProvider
. Ka anyị mepụta ugbu a ViewServiceProvider
iche iche.
Anyị nwere ike iji iwu a mepụta ya:
php artisan make:provider ViewServiceProvider
Kedu nke ga-eme ka klas ahụ pụta tupudefinite:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
Dị ka anyị nwere ike ịhụ n'ime, e nwere ụzọ abụọ:
Usoro ndekọ () na-enye anyị ohere definish njikọ na akpa ọrụ anyị. Dịka ọmụmaatụ, na koodu a:
public function register()
{
$this->app->singleton(my_class, function($app){
return new MyClass($app);
});
}
$ this->ngwa bụ mgbanwe zuru ụwa ọnụ na laravel nke otu klaasị singleton nwere ike nweta site na ngwa ahụ.
Singleton bụ atụmatụ. Mgbe ị na-etinye atụmatụ a, anyị na-agwa ngwa ahụ na klaasị ọ bụla agafere dị ka parameter na ngwa kwesịrị inwe naanị otu ihe atụ na ngwa niile. Nke a pụtara na MyClass ga-edozi otu ugboro ma nwee naanị otu ihe atụ, nke enwere ike ịnweta site na iji my_class variable.
Usoro buut() na-enye gị ohere ịnweta ọrụ niile edebanyelarị mbụ site na iji usoro ndebanye aha. Ị nwere ike tinye usoro a dum ọrụ na ngwa gị.
N'ịlaghachi na ihe atụ gara aga, ka anyị wepụ usoro ahụ register()
na n'ime boot()
tinye koodu ntuziaka Blade:
use Illuminate\Support\Facades\Blade;
public function boot()
{
Blade::directive('datetime', function ($expression) {
return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
});
}
Ihe atụ ọzọ nke ViewServiceProvider
nkwanye ugwu View Composers
, ebe a bụ snippet site na saịtị Laravel gọọmentị :
use App\View\Composers\ProfileComposer;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
public function boot()
{
// Using class based composers...
View::composer('profile', ProfileComposer::class);
// Using closure based composers...
View::composer('dashboard', function ($view) {
//
});
}
}
Iji na-agba ọsọ, a ga-atụkwasịrịrị/debanye aha onye na-eweta ọhụrụ a n'usoro ndị na-eweta config/app.php
:
return [
// ... other configuration values
'providers' => [
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
// Add your provider here
App\Providers\ViewServiceProvider::class,
],
];
Ercole Palmeri
Ị nwekwara ike ịmasị:
Coveware nke Veeam ga-aga n'ihu na-enye ọrụ nzaghachi ihe omume cyber. Coveware ga-enye forensics na ike mmezi…
Ndozi amụma na-agbanwe mpaghara mmanụ & gas, na-enwe usoro ọhụrụ na ngwa ngwa maka njikwa ihe ọkụkụ.…
UK CMA enyela ịdọ aka ná ntị gbasara omume Big Tech na ahịa ọgụgụ isi. Ebe ahụ…
Iwu “Case Green”, nke European Union chepụtara iji kwalite nrụpụta ike nke ụlọ, ejirila…