fk/laravel-utility is a Laravel package for making laravel more practical.
It currently has 1 GitHub stars and 136 downloads on Packagist (latest version v2.0.17).
Install it with composer require fk/laravel-utility.
Discover more Laravel packages by fk
or browse all Laravel packages to compare alternatives.
Last updated
Making laravel more practical
fk\utility\Database\Eloquent\ModelAdd approach to get the sql to be executed. It's available by calling
<?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
Modify pagination
toArraytoFKStyleModel::select related
Being able to using alias like following, see
\fk\utility\Database\Query\Builder::selectfor more
<?php
\fk\utility\Database\Eloquent\Model::select(['alias' => ['fields']]);
fk\utility\Http\Requestpublic/index.php# index.php, replace the default capture
$response = $kernel->handle(
$request = \fk\utility\Http\Request::capture()
);
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);
}
multipart/form-data for method PUTAllow session to be actually applied just when called. Not when requested. This is useful for RESTFul APIs, for some doesn't need a session.
<?php
class AppServiceProvider {
public function register()
{
$this->app->register(\fk\utility\Session\SessionServiceProvider::class);
}
}
<?php
[
'providers' => [
fk\utility\Session\SessionServiceProvider::class
]
];
Also remember cancel registering of the \Illuminate\Session\SessionServiceProvider
At last, you should set the config/session.php add
'auto_start' => true,
Also, remember to disable Laravel's start-on-every-request feature by comment the following if exists
# 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
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
]
]
];
Class: fk\utility\Foundation\Testing\TestCase
Benefits: Output for json would be human readable for Chinese characters
Usage:
<?php
use \fk\utility\Foundation\Testing\TestCase;
class YourTest extends TestCase
{
// Write your own `CreateApplication`
// OR
// Write a `createApplication` method here
use CreateApplication;
}
Class: fk\utility\Auth\Middleware\AclAuthenticate
Usages:
Create your own authentication class to place your rules
<?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
<?php
class Kernel
{
protected $routeMiddleware = [
'auth.acl' => \App\Http\Middleware\MyAuthenticate::class,
];
}
Good to go. Define a route using middleware auth.acl
<?php
Route::group(['middleware' => 'auth.acl'], function () {
Route::get('sth', 'SomeController@someMethod');
// ... stuff
});