michaeltintiuc / laravel-component by michaeltintiuc

Several starter interfaces and abstract classes for a laravel project
30
1
0
Package Data
Maintainer Username: michaeltintiuc
Maintainer Contact: contact@michaeltintiuc.com (Michael Tintiuc)
Package Create Date: 2016-12-13
Package Last Update: 2017-10-05
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2019-12-02 15:21:10
Package Statistics
Total Downloads: 30
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 0
Total Forks: 0
Total Open Issues: 1

laravel-component

:bulb: Several starter interfaces and abstract classes for laravel projects

Contents

  1. Installation
  2. Usage
  3. Contribution

Installation

Require via composer

composer require michaeltintiuc/laravel-component

Usage

Directory structure

app/Components/ - holds all component dirs like Users, Posts, Tags, etc

app/Components/Users - holds the model class (i.e. User) and all environment dirs like Admin, Site, etc

app/Components/Users/{ENV} - holds all classes and routes.php related to the environment specific implementation of a component

app/Components/Users/{ENV}/Requests - holds form validation classes specific to an environment and component


Controllers

namespace Acme\Components\Users\Admin;

use Illuminate\Http\Request;
use MichaelT\Component\Admin\ComponentController;

class UsersController extends ComponentController
{
    public function __construct(Request $request, PostTagsRepo $repo)
    {
        parent::__construct($request, $repo);
        $this->setComponent('user');
        $this->setBaseView('admin.users');
        $this->setSearchRoute('admin.users.index');
    }

    public function index(Request $request)
    {
        if ($request->has('search')) {
            return $this->search($request->search);
        }

        $this->setTitle('All users');
        $this->setHeading('Users list');
        $users = $this->repo->all();

        return $this->view('index')
            ->with(compact('users'));
    }
    
    ...
}

Repositories

Interfaces aka Contracts

namespace Acme\Components\Users\Admin;

use MichaelT\Component\Admin\Contracts\RepoContract;
use MichaelT\Component\Admin\Contracts\Searchable;

interface UsersRepoContract extends RepoContract, Searchable
{
}

Repository

namespace Acme\Components\Users\Admin;

use Acme\Components\Users\User;
use MichaelT\Component\Admin\ComponentRepo;
use Acme\Components\Users\Admin\UsersRepoContract;

class UsersRepo extends ComponentRepo implements UsersRepoContract
{
    public function __construct(User $model)
    {
        parent::__construct($model);
        $this->setComponent('user');
    }

    public function all()
    {
        return $this->model->get();
    }

    public function paginate()
    {
        return $this->model
            ->paginate($this->getPerPage());
    }

    public function find($id)
    {
        try {
            return $this->model->findOrFail($id);
        } catch (\Exception $e) {
            throw new \FindAdminException($this->error('find'));
        }
    }
    
    ...
}

Routes

Component

Route::group(['namespace' => 'Acme\Components\Users\Admin'], function () {
    Route::resource('users', 'UsersController');
});

Loading routes

Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
    Route::group(['middleware' => ['auth']], function () {
        require_once app_path().'/Components/Users/Admin/routes.php';
    });
});

Contribution

Contributions, bug-reports, feature and pull requests are always welcome!