Ragewa Middleware: Ƙididdiga Ingantaccen Ingantacciyar Middleware Ƙididdiga

Rage Middleware mataki ne mai mahimmanci don inganta ayyukan Laravel aikace-aikace. Middleware yana Laravel wakiltar matakan sarrafawa da aka yi kafin buƙatun su isa hanyoyin da aka keɓance. Koyaya, yin amfani da wuce gona da iri ko aiki mara inganci Middleware na iya ƙara lokacin sarrafa buƙatun da tasiri gabaɗayan aikin aikace-aikacen.

Anan akwai wasu hanyoyi don rage girman Middleware su da inganta su a cikin Laravel:

 

Gano Wajibi Middleware

Da farko, gano waɗanne Middleware ne masu mahimmanci ga takamaiman hanyoyi a cikin aikace-aikacen ku. Cire ko kashe ba dole ba Middleware na iya rage lokacin aiki mara amfani ga kowace buƙata.

 

Yi amfani da Rabawa Middleware

Idan hanyoyi da yawa suna raba saitin iri ɗaya na Middleware, la'akari da amfani da rabawa Middleware don sake amfani da su. Wannan yana taimakawa wajen guje wa maimaitawa kuma yana rage adadin da Middleware za a kashe.

 

Sharadi Middleware

Aiwatar Middleware kawai idan ya cancanta. Wani lokaci, ƙila kuna son aiwatarwa Middleware don takamaiman hanyoyi ko ƙungiyoyin hanya. Laravel yana ba ku damar amfani da sharadi Middleware don amfani da su don takamaiman lokuta.

// Middleware applied to routes in the 'admin' group  
Route::middleware(['admin'])->group(function() {  
    // Routes within the 'admin' group will execute the Middleware  
});  

 

Shirya Middleware cikin Ingantacciyar tsari

Middleware ana aiwatar da shi a cikin tsari da aka ayyana a cikin Kernel.php fayil ɗin. Tabbatar da shirya Middleware ta hanyar da za a fara aiwatar da mahimmanci da sauri, kuma ana sanya Middleware mai cin lokaci a ƙarshe. Middleware

protected $middlewarePriority = [  
    \App\Http\Middleware\FirstMiddleware::class,  
    \App\Http\Middleware\SecondMiddleware::class,  
    // ...  
];

 

Haɓaka Middleware a ciki yana Laravel taimakawa rage lokacin sarrafa buƙatun kuma yana haɓaka aikin aikace-aikacen gabaɗaya. Ta hanyar gano mahimmanci Middleware, amfani da su yadda ya kamata, da kuma yin la'akari da tsarin su, zaku iya haɓaka gabaɗayan tsarin sarrafa buƙatun a cikin aikace-aikacenku.