Controllers a Laravel- Gudanar da dabaru na aikace-aikacen da hulɗar bayanai

Controllers Akwai Laravel azuzuwan da ke da alhakin sarrafa dabaru na aikace-aikacen da sauƙaƙe hulɗar tsakanin samfura da ra'ayoyi. Controllers taimaka raba dabaru na aikace-aikace daga mai amfani, ƙirƙirar ingantaccen tsarin aikin da za a iya kiyayewa.

 

Ƙirƙiri mai sarrafawa

Don ƙirƙirar mai sarrafawa a cikin Laravel, zaku iya amfani da Laravel umarnin Artisan. Misali, don ƙirƙirar mai sarrafawa mai suna UserController, zaku iya gudanar da umarni mai zuwa a cikin tasha:

php artisan make:controller UserController

Da zarar an ƙirƙiri mai sarrafawa, zaku iya ayyana hanyoyin sarrafawa a cikin mai sarrafawa. Misali, a cikin index() hanyar, zaku iya dawo da bayanai daga samfuri kuma ku wuce shi zuwa kallo don nunawa:

namespace App\Http\Controllers;  
  
use App\Models\User;  
use Illuminate\Http\Request;  
  
class UserController extends Controller  
{  
    public function index()  
    {  
        $users = User::all();  
  
        return view('users.index', ['users' => $users]);  
    }  
  
    // Other handling methods  
}  

A cikin misalin da ke sama, muna amfani da User ƙirar don dawo da bayanan mai amfani daga ma'ajin bayanai. Sa'an nan kuma mu wuce wannan bayanan zuwa ra'ayi users.index don nuna jerin masu amfani.

Controllers Hakanan goyan bayan hanyoyin kamar store(), update(), da kuma delete() sarrafa ƙirƙira, ɗaukakawa, da gogewa. Kuna iya hulɗa tare da ma'ajin bayanai ta waɗannan hanyoyin.

 

HUsing controller in route

Don amfani da a controller cikin route, zaku iya ƙididdige controller suna da hanyar da ta dace a cikin routes/web.php fayil ɗin.

use App\Http\Controllers\UserController;  
  
Route::get('/users', [UserController::class, 'index']);  

A cikin wannan misalin, lokacin da mai amfani ya shiga /users URL, Laravel zai kira index() hanyar da ke cikin UserController don karɓar buƙatar.

 

Ƙirƙiri kallo don allon lissafin masu amfani

Don ƙirƙirar users.index fayil ɗin, zaku iya amfani da umarni mai zuwa:

php artisan make:view users.index

Wannan umarnin zai haifar da index.blade.php fayil a cikin resources/views/users kundin adireshi.

Da zarar an ƙirƙiri fayil ɗin, zaku iya buɗe index.blade.php fayil ɗin kuma ku ƙirƙira abin dubawa don users.index shafin. Za ka iya amfani da Blade syntax don ƙirƙirar tsarin HTML da nuna bayanai daga mai sarrafawa.

<!-- resources/views/users/index.blade.php -->  
@extends('layouts.app')  
  
@section('content')  
    <h1>Users</h1>  
    <ul>  
        @foreach($users as $user)  
            <li>{{ $user->name }}</li>  
        @endforeach  
    </ul>  
@endsection  

A cikin misalin da ke sama, muna amfani da app.blade.php shimfidar wuri ta hanyar @extends('layouts.app'). An bayyana abun ciki na shafin a ciki @section('content') kuma yana nuna jerin masu amfani daga $users madaidaicin cikin madauki @foreach.

Don amfani da users.index shafin, kuna buƙatar ayyana hanyar da ta dace a cikin routes/web.php fayil don nuna hanyar a cikin mai sarrafawa kuma dawo da users.index ra'ayi.

 

A taƙaice, taimako controllers don Laravel ware dabaru na aikace-aikacen da sarrafa sarrafa bayanai. Ta amfani da controllers, zaku iya gina ƙaƙƙarfan aikace-aikace masu ƙarfi a cikin Laravel.