Skip to content
💡 Several starter interfaces and abstract classes for laravel projects
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.styleci.yml
LICENSE
README.md
composer.json

README.md

laravel-component

💡 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!

You can’t perform that action at this time.