tychovbh/laravel-mvc

Add mvc structure to laravel

v3.3 2021-01-26 09:55 UTC

README

Latest Version on Packagist Software License Total Downloads

Laravel MVC is created by, and is maintained by Tycho, and is a Laravel/Lumen package to manage all your data via a Repository. Feel free to check out the change log, releases, license, and contribution guidelines

Install

Via Composer

$ composer require tychovbh/laravel-mvc

For lumen application add Service Provider to bootstrap/app.php

$app->register(\Tychovbh\Mvc\MvcServiceProvider::class);

Usage

Repositories

Create a Repository:

// Creates a repository in app/Repositories
artisan mvc:repository UserRepository

Use The UserRepository in controller, but you can use it anywhere else too.

class UserController extends AbstractController
{
    public function __construct(UserRepository $repository)
    {
        $this->repository = $repository
    }
    
    public function index()
    {
        $users = $this->repository->all();
        return response()->json($users)
    }
}

Make sure you have a Model that your repository can use. If you want to use save/update methods add $filled to your model.

class User extends model
{
    protected $filled = ['name']
}

Available methods"

// Get all
$this->repository->all();

// Search all resources where name: Jan
$this->repository::withParams(['name' => 'Jan'])->get();

// Search all resources where name: Jan and get first.
$this->repository::withParams(['name' => 'Jan'])->first();

// Search all resources where names in: Jan and piet
$this->repository::withParams(['name' => ['jan', 'piet']])->get();

// Order all resources by name or any other Laravel statement
$this->repository::withParams(['sort' => 'name desc')]->get();

// Paginate 10
$this->repository->paginate(10);

// Paginate 10 where country in Netherlands or Belgium.
$this->repository::withParams(['country' => ['Netherlands', 'Belgium']])->paginate(4);

// Search resource with ID: 1
$this->repository->find(1);

// Store resource in the database. This uses laravel fill make sure you add protected $filled = ['name'] to your User model.
$user = $this->repository->save(['name' => 'jan']);

// Update resource.
$user = $this->repository->update(['name' => 'piet'], 1);

// Destroy resource(s).
$this->repository->destroy([1]);

If you wish to override on of the methods above just add it to you repository

class UserRepository extends AbstractRepository implements Repository
{
    public function find(int $id)
    {
        // add your own implementation of find
        return $user;
    }
    
    public function save($data)
    {
        // Add some logic and then call parent save
        $data['password'] = Hash:make($data['password']);
        return parent::save($data);
    }
    
    // You can add your own custom params to filter the request
    // This will be triggered when key is "search" is added to the params:
    // Let's say we want to build a search on firstname, lastname and email:
    // $repository->params(['search' => 'jan'])->all();
    // $repository->params(['search' => 'jan@gmail.com'])->all();
    // $repository->params(['search' => 'piet'])->all();
    // We can do that by adding a method, just capitalize the param key and add index{key}Param to the method name.
    public function indexSearchParam(string $search)
    {
        $this->query->where('email', $search)
                    ->orWhere('firstname', $search)
                    ->orWhere('surname', $search);    
    }
    
    // You can do the same for show methods like find
    public function showSearchParam(string $search);
}

Controllers

Create a Controller:

// Creates a Controller in app/Http/Controllers
artisan mvc:controller UserController

All Laravel Resource methods are now available (index, show, store, update, destroy). See their documentation here for setting up routes: laravel resource controllers.

You can override Resource methods to do project related stuff

class UserController extends AbstractController
{
    public function index()
    {
        // Do stuff before querying
        
        $response = parent::index();

        // Do stuff after querying

        return $response;
    }
}

Form Requests

Create a Form Request:

// Creates a Form Request in app/Http/Requests
artisan mvc:request StoreUser

You can use the request middleware "valdiate" to validate the request. It will look for the FormRequest and validate it So for example model User:

  • store request (POST /users) will look for a FormRequest with name StoreUser
  • update request (UPDATE /user/{id}) will look for a FormRequest with name UpdateUser
// routes/web.php (Laravel)
$router->post('/users', 'UserController@index')
->name('user.index')
->middleware('validate');

// routes/web.php (Lumen)
$router->post('/users', [
    'middleware' => 'validate',
    'as' => 'users.index',
    'uses' => 'UserController@index'
]);

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email info@bespokeweb.nl instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.