ahmedalmory / joda-resources

一个生成控制器资源方法的特质

v5.0.4 2023-01-31 09:31 UTC

README

Latest Version on Packagist Total Downloads

一个生成控制器资源方法的特质。

安装

您可以通过composer安装此包

composer require ahmedalmory/joda-resources

使用

JodaResource

web.php

Route::resource('/users', 'UserController');

UserController.php

<?php

// any thing after "controllers" in namespace would be prefixed to view and route properties
// Ex. namespace App\Http\Controllers\Admin;
// View would be admin.user
// route would be admin.users
namespace App\Http\Controllers;

use AhmedAlmory\JodaResources\JodaResource;
use App\Models\User;

class UserController extends Controller
{

     use JodaResource;

     // model that will be used for crud operations
     protected $model = User::class;
     // JodaResources will try to find a model with the name User in App\Models, App\ or App\Model


     // will be used in store and update validation in case storeRules or updateRules are not set
     protected $rules = ['name' => 'required', 'email' => 'sometimes'];
     // required either in the controller or the model

    // will be used for store validation, if set
    // public static $storeRules =[];

    // will be used for update validation , if set
    // public static $updateRules =[];


     // optional
     // will be the name of the model in lower case if not set in this example 'user'
     protected $name = 'user';
     // name of the model that will be used in views ans routes in case there are not set


     // optional
     // will be the name of the model (in kebab case in case more than one word) if not set in this example 'user'
     protected $view = 'user';
     // name of the model that will be used in returned views


     /// optional
     // will be plural of the name property (in kebab case in case more than one word) if not set in this example 'users'
     protected $route = 'users';
     // name of the model that will be used in returned routes after finishing the operation


     // optional
     protected $files = ['photo'];
     // items will be uploaded from the request in case there is file with the same name
     // files will be saved in /uploads/{pluralNameOfTheModel} with name {user_id}-{time}.{ext}
     // ex uploads/users/1-1624479228.jpg
     // file will be deleted automatically upon deleting the object
     
     // true by default
     prtected $filterQueryString = true;

     // add custom query
     public function query($query)
     {
         return $query->whereNotNull('another_filed')->get();
     }
}

//methods will be provided

//index => will return view($view) with three variables, 'route' route of the resource to be used in actions, 'index' (all users) and plural name of the model in this example 'users' you can use either of them

//create => will return view($view.create)

//store => will save all cols from request then return to $route.index

//show => will return view($view.show ) with two variables, 'show' and name of the model in this example 'user' you can use either of them

//edit => will return view($view.edit) with tow variables, 'edit' and name of the model in this example 'user' you can use either of them

//update => will update all cols from request then return to $route.index

//destroy => will save all cols from request then return to $route.index

JodaApiResource

JodaApiResource 与 JodaResource 上述选项相同

api.php

Route::resource('/examples', 'ExampleController');

ExampleController.php

namespace App\Http\Controllers\Api;

use AhmedAlmory\JodaResources\JodaApiResource;

class ExampleController extends Controller
{
 use JodaApiResource;

    protected $rules = [
        'filed' =>'required',
        'another_filed' => 'sometimes'
    ];
}

index => get => example.com/api/examples?field=queryStringExample

store => post => example.com/api/examples

show => get => example.com/api/examples/1

update => put => example.com/api/examples/1

destroy => delete => example.com/api/examples/1

定制化

有定制化方法,如 beforeStore() 在将数据存储到数据库之前触发,afterStore() 在存储数据之后触发,例如,您可以更改闪存消息或重定向到其他页面,对于更新和删除,有 beforeUpdate()、afterUpdate()、beforeDestroy() 和 afterDestroy() 等类似的方法。

更新日志

请参阅 更新日志 了解最近更改的详细信息。

贡献

请参阅 贡献指南 了解详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件 ahmedalmory02@gmail.com 而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。请参阅 许可证文件 了解更多信息。