该包已被废弃且不再维护。未建议替代包。
Laravel 5 生成器。
0.0.2
2015-11-23 03:53 UTC
Requires
- php: >=5.4.0
- illuminate/console: ~5.0
- illuminate/filesystem: ~5.0
- illuminate/support: ~5.0
Requires (Dev)
- phpspec/phpspec: ~2.1
This package is not auto-updated.
Last update: 2022-02-01 12:50:22 UTC
README
基于Jeffery Way的出色生成器,我们在3rd Sense中添加了几个我们经常使用的生成器。
这些包括
- make:repository
- make:transformer
复制官方仓库Illuminate\Foundation中的make命令,用于Lumen。
- make:command
- make:console
- make:event
- make:job
- make:listener
- make:model
- make:provider
- make:test
使用方法
步骤 1:使用 composer
安装
composer require 3rd-sense/generators --dev
步骤 2:添加服务提供者
根据Jeffery的建议,这些命令应仅存在于本地开发环境中,因此您不需要更新生产环境中的 providers
数组 config/app.php
。相反,在 app/Providers/AppServiceProvider.php
中添加提供者,如下所示
Laravel config/app.php
public function register() { if ($this->app->environment() == 'local') { $this->app->register('ThirdSense\Generators\LaravelServiceProvider'); } }
Lumen bootstrap/app.php
if ($app->environment() === 'local') { $app->register(\ThirdSense\Generators\LumenServiceProvider::class); }
步骤 3:运行 artisan
生成器!
您已设置好。在控制台中运行 php artisan
,您将在 make:*
命名空间部分看到新命令。
示例
对于所有其他命令,请参阅官方的 Laravel 文档
生成存储库类
php artisan make:repository UserRepository App/User
这将在 app/repositories 目录中为您生成两个文件,存储库
<?php namespace App\Repositories; use App\User; /** * Class UserRepositoryRepository * @package App\Repositories */ class UserRepository implements UserRepositoryInterface { /** * Retrieve all User. * * @return mixed */ public function all() { return User::all(); } /** * Retrieve a paginated list of User. * * @param $limit */ public function paginated($limit) { return User::paginate($limit); } /** * Retrieve a single User by ID. * * @param $id * * @return mixed */ public function find($id) { return User::find($id); } /** * Create and save a new User. * * @param $data * * @return bool */ public function create($data) { $entity = new User; return $this->save($entity, $data); } /** * Update an existing User. * * @param $id * @param $data * * @return User */ public function update($id, $data) { $entity = $this->findById($id); return $this->save($entity, $data); } /** * Remove/delete exiting User * * @param $id * * @return int */ public function destroy($id) { return User::destroy($id); } /** * Save the User. * * @param $entity * @param $data * * @return boolean */ protected function save($entity, $data) { // set model properties return $entity->save(); } }
和存储库接口
<?php namespace App\Repositories; use App\User; /** * Interface UserRepositoryInterface * @package App\Repositories */ interface UserRepositoryInterface { /** * Retrieve all User. * * @return mixed */ public function all(); /** * Retrieve a paginated list of User. * * @param $limit */ public function paginated($limit); /** * Retrieve a single User by ID. * * @param $id * * @return mixed */ public function find($id); /** * Create and save a new User. * * @param $data * * @return bool */ public function create($data); /** * Update an existing User. * * @param $id * @param $data * * @return User * */ public function update($id, $data); /** * Remove/delete exiting User * * @param $id * * @return int */ public function destroy($id); }
现在您只需将这些存储库注册到您的服务容器中。为此,只需将以下代码片段添加到您的 app/Providers/AppServiceProvider
的 register
方法中
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
然后您就可以开始了。
另一种选择是创建一个 App/Providers/RepositoriesServiceProvider
类,并将上述代码放在 register
方法中。请记住将此新的服务提供者添加到您的 config/app.php
的 providers
数组中。
生成转换器类
此生成器用于创建由 The PHP League 提供的 Fractal 包的转换器。
php artisan make:transformer UserTransformer App/User
这将在 app/Transformers 目录中为您生成以下转换器
<?php namespace App\Transformers; use App\User; use League\Fractal\TransformerAbstract; class UserTransformer extends TransformerAbstract { /** * List of resources to possibly include * * @var array */ protected $availableIncludes = []; /** * List of resources to automatically include * * @var array */ protected $defaultIncludes = []; /** * @param User $user * * @return array */ public function transform(User $user) { return [ 'id' => $user->id, // TODO: transform entity properties 'links' => [ [ 'rel' => 'self', 'uri' => route('user.show', ['id' => $user->id]), ], [ 'rel' => 'list', 'uri' => route('user.index'), ], ], ]; } }