Utilities
Making laravel more practical, (*1)
Database
Model
- Usage: Just extends
fk\utility\Database\Eloquent\Model
-
functionality, (*2)
-
Add approach to get the sql to be executed.
It's available by calling, (*3)
<?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;
In fact, it works for any method that returns a fk\utility\Database\Eloquent\Builder, (*4)
-
Modify pagination, (*5)
- add access to custom fields when calling
toArray
- add access to
toFKStyle
-
Model::select related, (*6)
Being able to using alias like following,
see \fk\utility\Database\Query\Builder::select for more, (*7)
<?php
\fk\utility\Database\Eloquent\Model::select(['alias' => ['fields']]);
Request
- Class
- Usage
- Capture at
public/index.php
# index.php, replace the default capture
$response = $kernel->handle(
$request = \fk\utility\Http\Request::capture()
);
- Extends or use it for IOC
- Register it's alias as
request,
to ensure every instance fallback to the singleton instance
used to capture at entry index.php
#
# AppServiceProvider.php
public function reigster()
{
$this->app->alias('request', \fk\utility\Http\Request::class);
}
- Functionality
- Add support for Content-Type
multipart/form-data for method PUT
Session
Allow session to be actually applied just when called. Not when requested.
This is useful for RESTFul APIs, for some doesn't need a session., (*8)
AppServiceProvider
<?php
class AppServiceProvider {
public function register()
{
$this->app->register(\fk\utility\Session\SessionServiceProvider::class);
}
}
or add to config/app.php
<?php
[
'providers' => [
fk\utility\Session\SessionServiceProvider::class
]
];
Also remember cancel registering of the \Illuminate\Session\SessionServiceProvider, (*9)
At last, you should set the config/session.php add, (*10)
'auto_start' => true,
Also, remember to disable Laravel's start-on-every-request feature
by comment the following if exists, (*11)
# app\Http\Kernel
public $middlewares = [
// \Illuminate\Session\Middleware\StartSession::class,
]
If you have your own rule of session id,
you can overwrite the \fk\utility\Session\SessionServiceProvider::getAccessToken
to achieve that, (*12)
Easy Authentication
Register Service Provider
fk\utility\Auth\Session\SessionGuardServiceProvider, (*13)
Config
<?php
# auth.php
return [
'guards' => [
'api' => [
'driver' => 'easy.token',
'model' => \App\Models\User::class, // The model to retrieve user from
]
]
];
PHPUnit
TestCase
ACL Check
- Class:
fk\utility\Auth\Middleware\AclAuthenticate
-
Usages:, (*15)
-
Create your own authentication class to place your rules, (*16)
<?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
}
}
-
Register at App\Http\Kernel, (*17)
<?php
class Kernel
{
protected $routeMiddleware = [
'auth.acl' => \App\Http\Middleware\MyAuthenticate::class,
];
}
-
Good to go. Define a route using middleware auth.acl, (*18)
<?php
Route::group(['middleware' => 'auth.acl'], function () {
Route::get('sth', 'SomeController@someMethod');
// ... stuff
});