squibler / laravel-artisan
干净地设置laravel
0.2.5
2018-09-23 11:54 UTC
This package is auto-updated.
Last update: 2024-09-24 04:49:29 UTC
README
当您希望模型默认在 app/Models
目录下创建时使用。
- 修改
make:model
以使用app/Models
目录和App\Models
命名空间 - 更新 artisan 命令
php artisan make:model ModelName
,以便在具有正确命名空间的目录中 - 更新
php artian make:controller Controller -m
命令以使用更新的模型结构 - 添加新的逻辑命令
php artisan make:logic Name
以创建业务逻辑类
使用方法
1. 使用 composer 将其安装为开发依赖项
composer require --dev squibler/laravel-artisan
2. 模型和控制器
Artisan 的使用与捆绑的 Laravel 命令没有变化,实际上,在这个包中,它们只是扩展了以改变默认路径或添加一些新技巧
新的默认路径
对于模型,定义了一个新的基本路径并命名空间化,以便在专用的 app/Models 目录中创建模型,而不是像默认的 Laravel 行为一样在 app 根目录中创建。
控制器仍然使用默认设置,因此没有变化。
新选项
两者 make:controller
和 make:model
artisan 命令都得到了一个新的 --logic
(或更短的 -l
)选项。
此选项告诉命令同时创建与正在创建的模型或控制器一起的业务逻辑类。
3. 业务逻辑类(可选使用)
业务逻辑类在新的 app/Logic
目录中创建,并可能从模型扩展
您可以使用以下命令创建逻辑类: php artisan make:logic Name [--options]
使用方法
php artisan make:logic [options] Name
可用选项
--model[=Model] | -m [Model] Create Model if it does not exist and
then extend the Logic from the model.
--controller | -c Create an API Controller `NameController` for the logic
--migration | -d Create a new migration at the same time
--all Do all of the above
逻辑类的想法是它们可以是单次使用的。它们可以扩展模型或独立使用。它们可以根据需要使用许多资源。
通常,逻辑类位于控制器和模型之间,有助于保持两者更干净、更易于阅读、更有目的性。
app/Http/Controllers/MyController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyController extends Controller
{
public function umbrella( Request $request )
{
// No Business Logic here only relationships
return ShouldTakeUmbrellaLogic::byRequest($request);
}
}
app/Logic/UmbrellaLogic.php
<?php
namespace App\Logic;
use Illuminate\Http\Request;
use App\Models\Umbrella;
class ShouldTakeUmbrellaLogic
{
public static function byRequest( Request $request )
{
if (! $address = $request->address) {
$user = $request->user();
$address = $user->address;
}
$forecast = WeatherForecastLogic::byAddress($address);
return ($forecast->rain);
}
}