saritasa / laravel-controllers
Saritasa controllers for typical operations
Installs: 14 488
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 8
Forks: 4
Open Issues: 0
Requires
- php: >=8.0
- ext-json: *
- laravel/framework: ^9.0 | ^10.0
- laravel/ui: ^4.0
- saritasa/laravel-fluent-validation: ^1.3
- tymon/jwt-auth: ^2.0
Requires (Dev)
- mockery/mockery: ^1.1
- phpunit/phpunit: >=9.0
- squizlabs/php_codesniffer: ^3.0
- dev-master / 5.x-dev
- 5.0.1
- 5.0.0
- 4.1.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.4.0
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.0.x-dev
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-3_3_5_extension
- dev-bugfix/#14-wrong-router-behavior-with-custom-route-key
- dev-maxermolenko-patch-1
This package is auto-updated.
Last update: 2024-09-07 08:14:55 UTC
README
Controllers for common UI and endpoints in Laravel,
like API authentication, password change, login page, etc.
Laravel 9.x/10.x
Install the saritasa/laravel-controllers
package:
$ composer require saritasa/laravel-controllers
Available controllers
There are 2 types of controllers:
- Web - interactive UI for user - traditional Laravel controllers.
Many of them just provide out-of-the-box Laravel functionality,
using built-in traits. - Api - for programmatic integration with 3d party applications,
like mobile apps (iOS, Android) or single-page HTML applications,
built on modern frontend frameworks - React.JS, AngularJS, VueJs, etc.
API utilizes Dingo/Api library
and custom extensions for it: saritasa/dingo-api-custom
Controllers, described below, exist, but you must register routes for them manually
Methods
- function json($data, IDataTransformer $transformer = null): Response
Example:
class UserApiController extends BaseApiController { public function __construct(UserTransformer $userTransformer) { parent::__construct($userTransformer); } public function editUserProfile(Request $request, User $user): Response { $this->validate($request, $user->getRuels()); $user->fill($request->all()); $user->save(); return $this->json($user); } }
JWTAuthApiController Authenticate API Controller. Uses JWT authentication
Utilizes Dingo\Api JWT Auth
settings and underlying tymon\jwt-auth
Example: routes\api.php:
app('api.router')->version(config('api.version'), ['namespace' => 'Saritasa\Laravel\Controllers\Api'], function (\Dingo\Api\Routing\Router $api) { // Authentication $api->post('auth', 'AuthController@login'); // Login $api->put('auth', 'AuthController@refreshToken'); // Refresh expired token $api->delete('auth', 'AuthController@logout')->middleware('api.auth'); // Logout });
Customize login request
In some case, we're using email
field for login with email
or username
in application. So, the email
field should validation by required
and string
rule.
Or you want to use username
instead of email
.
How to bind ILoginRequest with custom request class
<?php namespace App\Providers; use App\Http\Requests\Auth\LoginRequest; use Illuminate\Support\Facades\URL; use Illuminate\Support\ServiceProvider; use Saritasa\LaravelControllers\Requests\Concerns\ILoginRequest; class AppServiceProvider extends ServiceProvider { public function boot() { $this->app->bind(ILoginRequest::class, LoginRequest::class); } }
<?php namespace App\Http\Requests\Auth; use Illuminate\Foundation\Http\FormRequest; use Saritasa\LaravelControllers\Requests\Concerns\ILoginRequest; class LoginRequest extends FormRequest implements ILoginRequest { /** * @inheritDoc */ public function rules(): array { return [ 'username' => 'required|string', 'password' => 'required|string', ]; } }
ForgotPasswordApiController, ResetPasswordApiController These controllers are responsible for handling password reset emails.
Utilize native Laravel password management without UI, in JSON API.
Example: routes\api.php:
app('api.router')->version(config('api.version'), ['namespace' => 'Saritasa\Laravel\Controllers\Api'], function (\Dingo\Api\Routing\Router $api) { $api->post('auth/password/reset', 'ForgotPasswordApiController@sendResetLinkEmail'); $api->put('auth/password/reset', 'ResetPasswordApiController@reset'); });
Contributing
- Create fork, checkout it
- Develop locally as usual. Code must follow PSR-1, PSR-2 -
run PHP_CodeSniffer to ensure, that code follows style guides - Cover added functionality with unit tests and run PHPUnit to make sure, that all tests pass
- Update README.md to describe new or changed functionality
- Add changes description to CHANGES.md file. Use Semantic Versioning convention to determine next version number.
- When ready, create pull request
Make shortcuts
If you have GNU Make installed, you can use following shortcuts:
make cs
(instead ofphp vendor/bin/phpcs
) -
run static code analysis with PHP_CodeSniffer
to check code stylemake csfix
(instead ofphp vendor/bin/phpcbf
) -
fix code style violations with PHP_CodeSniffer
automatically, where possible (ex. PSR-2 code formatting violations)make test
(instead ofphp vendor/bin/phpunit
) -
run tests with PHPUnitmake install
- instead ofcomposer install
*make all
or justmake
without parameters -
invokes described above install, cs, test tasks sequentially -
project will be assembled, checked with linter and tested with one single command