tychovbh / laravel-mvc
Add mvc structure to laravel
Installs: 2 272
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 1
Open Issues: 1
Requires
- php: ^7.1.3
- ext-json: *
- anam/phantomjs-linux-x86-binary: ^2.1
- anam/phantommagick: ^2.0
- chelout/offset-pagination: dev-master
- guzzlehttp/guzzle: ^7.2
- illuminate/console: ~5.6.0|~5.7.0|~5.8.0|~5.9.0|~6.0
- illuminate/contracts: ~5.6.0|~5.7.0|~5.8.0|~5.9.0|~6.0
- illuminate/database: ~5.6.0|~5.7.0|~5.8.0|~5.9.0|~6.0
- illuminate/http: ~5.6.0|~5.7.0|~5.8.0|~5.9.0|~6.0
- illuminate/mail: ~5.6.0|~5.7.0|~5.8.0|~5.9.0|~6.0
- illuminate/support: ~5.6.0|~5.7.0|~5.8.0|~5.9.0|~6.0
- mollie/laravel-mollie: ^2.0
- rbdwllr/reallysimplejwt: ^2.0
- tychovbh/laravel-package-skeleton: ^1.3
- urameshibr/lumen-form-request: ^1.5
Requires (Dev)
- mockery/mockery: ^1.2
- orchestra/testbench: ^3.8
- phpunit/phpunit: ^8.1
- squizlabs/php_codesniffer: ^2.3
- v3.3
- v3.2
- v3.1
- 3.0.x-dev
- v3.0
- v2.5
- v2.4
- v2.3
- v2.2
- v2.1
- v2.0
- v1.9
- v1.8
- v1.7
- v1.6
- v1.5
- v1.4
- v1.3
- v1.2
- v1.1
- dev-master / 1.0.x-dev
- v1.0
- v0.9
- v0.8
- v0.7
- v0.6
- v0.5
- v0.4
- v0.3
- v0.2
- v0.1
- dev-develop
- dev-feature/database_crawler
- dev-feature/revisions
- dev-feature/automatic-import-voucher-codes
- dev-feature/add_contracts
This package is auto-updated.
Last update: 2024-10-14 17:18:09 UTC
README
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.