Jagoran Controller Repository Service Model zuwa Laravel

Babban jagorar aiwatarwa don Controller- Repository- Service model in Laravel yana taimaka muku tsara lambar tushe ta hanyar da ke da sauƙin sarrafawa da kulawa. Ga cikakken misali na yadda zaku iya aiwatar da wannan tsarin:

Model

Wannan shi ne inda kuke ayyana halaye da hanyoyin yin hulɗa tare da bayanan bayanai. Laravel yana ba da hanyar ORM mai ƙarfi don aiki tare da samfura. Misali, bari mu ƙirƙiri tebur model don Posts tebur:

// app/Models/Post.php  
namespace App\Models;  
  
use Illuminate\Database\Eloquent\Model;  
  
class Post extends Model  
{  
    protected $fillable = ['title', 'content'];  
}  

Repository

Yana repository aiki azaman tsaka-tsaki Controller tsakanin Model. Ya ƙunshi hanyoyin yin ayyukan adana bayanai ta hanyar model. Wannan yana taimakawa wajen raba dabaru na bayanan bayanai daga na controller kuma yana sauƙaƙa canzawa ko gwada dabarun bayanai.

// app/Repositories/PostRepository.php  
namespace App\Repositories;  
  
use App\Models\Post;  
  
class PostRepository  
{  
    public function create($data)  
    {  
        return Post::create($data);  
    }  
      
    public function getAll()  
    {  
        return Post::all();  
    }  
      
    // Other similar methods  
}  

Service

Yana service ƙunshe da dabaru na kasuwanci kuma yana sadarwa tare da Repository. Za Controller a kira hanyoyin da Service za a yi amfani da buƙatun da dawo da bayanai masu dacewa. Wannan yana taimakawa wajen raba dabaru na kasuwanci da kuma controller sa gwaji da kiyayewa cikin sauƙi.

// app/Services/PostService.php  
namespace App\Services;  
  
use App\Repositories\PostRepository;  
  
class PostService  
{  
    protected $postRepository;  
      
    public function __construct(PostRepository $postRepository)  
    {  
        $this->postRepository = $postRepository;  
    }  
      
    public function createPost($data)  
    {  
        return $this->postRepository->create($data);  
    }  
      
    public function getAllPosts()  
    {  
        return $this->postRepository->getAll();  
    }  
      
    // Other similar methods  
}  

Controller

A controller nan ne kuke gudanar da buƙatun mai amfani, hanyoyin kira daga wurin Service don dawo da ko aika bayanai, da mayar da sakamako ga mai amfani.

// app/Http/Controllers/PostController.php  
namespace App\Http\Controllers;  
  
use Illuminate\Http\Request;  
use App\Services\PostService;  
  
class PostController extends Controller  
{  
    protected $postService;  
      
    public function __construct(PostService $postService)  
    {  
        $this->postService = $postService;  
    }  
      
    public function create(Request $request)  
    {  
        $data = $request->only(['title', 'content']);  
        $post = $this->postService->createPost($data);  
        // Handle the response  
    }  
      
    public function index()  
    {  
        $posts = $this->postService->getAllPosts();  
        // Handle the response  
    }  
      
    // Other similar methods  
}  

Ta hanyar amfani da wannan tsarin, zaku iya sarrafa sassa daban-daban na Laravel aikace-aikacenku yadda yakamata. Bugu da ƙari, ware dabaru na kasuwanci, dabaru na ajiya, da sadarwa tsakanin azuzuwan yana sa lambar lambar ku ta zama mai sassauƙa, mai kiyayewa, da abin iya gwadawa.