Controllers for common UI and endpoints in Laravel,
like API authentication, password change, login page, etc.
Install the saritasa/laravel-controllers
package:
$ composer require saritasa/laravel-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
Recommended to use as base controller for other API controllers.
- 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);
}
}
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
});
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');
});
- 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
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