fk / laravel-utility
此包最新版本(v2.0.17)没有可用的许可证信息。
使Laravel更加实用
v2.0.17
2020-01-31 09:15 UTC
Requires
- fk/http-status: *
- laravel/framework: >=5
This package is not auto-updated.
Last update: 2024-09-23 22:58:25 UTC
README
使Laravel更加实用
数据库
模型
- 使用方法:只需扩展
fk\utility\Database\Eloquent\Model
- 功能
-
添加获取要执行的SQL的方法。可以通过调用
<?php use fk\utility\Database\Eloquent\Model; /** @var \fk\utility\Database\Eloquent\Builder $model */ $model = Model::find(1); $model->rawSql(); // or simply call, witch applies the __toString method echo $model;
实际上,它适用于返回
fk\utility\Database\Eloquent\Builder
的任何方法 -
修改分页
- 在调用
toArray
时添加对自定义字段的访问 - 添加对
toFKStyle
的访问
- 在调用
-
Model::select 相关
能够使用以下别名,更多请参考
\fk\utility\Database\Query\Builder::select
<?php \fk\utility\Database\Eloquent\Model::select(['alias' => ['fields']]);
-
请求
- 类
fk\utility\Http\Request
- 使用方法
- 在
public/index.php
中捕获
# index.php, replace the default capture $response = $kernel->handle( $request = \fk\utility\Http\Request::capture() );
- 扩展或用于IOC
- 将其别名注册为
request
,以确保每个实例回退到在入口index.php中捕获的单例实例
# AppServiceProvider.php public function reigster() { $this->app->alias('request', \fk\utility\Http\Request::class); }
- 在
- 功能
- 为
PUT
方法添加对multipart/form-data
内容类型的支持
- 为
会话
允许会话仅在调用时实际应用,而不是在请求时。这对于RESTFul API非常有用,因为某些API不需要会话。
AppServiceProvider
<?php class AppServiceProvider { public function register() { $this->app->register(\fk\utility\Session\SessionServiceProvider::class); } }
或添加到 config/app.php
<?php [ 'providers' => [ fk\utility\Session\SessionServiceProvider::class ] ];
还要记住取消注册 \Illuminate\Session\SessionServiceProvider
最后,您应该在 config/session.php 中设置
'auto_start' => true,
还要,记得禁用Laravel的每个请求启动功能,如果存在的话,请取消注释以下内容
# app\Http\Kernel
public $middlewares = [
// \Illuminate\Session\Middleware\StartSession::class,
]
如果您有自己的会话ID规则,您可以通过重写 \fk\utility\Session\SessionServiceProvider::getAccessToken
来实现
简单认证
注册服务提供者
fk\utility\Auth\Session\SessionGuardServiceProvider
配置
<?php # auth.php return [ 'guards' => [ 'api' => [ 'driver' => 'easy.token', 'model' => \App\Models\User::class, // The model to retrieve user from ] ] ];
PHPUnit
测试用例
-
类:
fk\utility\Foundation\Testing\TestCase
-
优点:对于中文字符,json的输出将是可读的
-
使用方法
<?php use \fk\utility\Foundation\Testing\TestCase; class YourTest extends TestCase { // Write your own `CreateApplication` // OR // Write a `createApplication` method here use CreateApplication; }
ACL检查
-
类:
fk\utility\Auth\Middleware\AclAuthenticate
-
使用方法
-
创建自己的认证类以放置您的规则
<?php namespace App\Http\Middleware; use fk\utility\Auth\Middleware\AclAuthenticate; class MyAuthenticate extends AclAuthenticate { public function authenticate(): bool { // Write your own authentication here // If false returned, a `Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException` exception will be thrown // otherwise, authentication will pass. // Feel free to throw any kind of exceptions that fits you } }
-
在
App\Http\Kernel
中注册<?php class Kernel { protected $routeMiddleware = [ 'auth.acl' => \App\Http\Middleware\MyAuthenticate::class, ]; }
-
准备好了。定义一个使用中间件
auth.acl
的路由<?php Route::group(['middleware' => 'auth.acl'], function () { Route::get('sth', 'SomeController@someMethod'); // ... stuff });
-