Skip to content

pnlinh/php-laravel-controllers

 
 

Repository files navigation

Laravel Controllers

PHP Unit PHP CodeSniffer codecov Release PHPv Downloads

Controllers for common UI and endpoints in Laravel,
like API authentication, password change, login page, etc.

Laravel 5.7+/6.0

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

BaseApiController Base API controller, utilizing helpers from Dingo/API package.

Recommended to use as base controller for other API controllers.

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  
  });

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

  1. Create fork, checkout it
  2. Develop locally as usual. Code must follow PSR-1, PSR-2 -
    run PHP_CodeSniffer to ensure, that code follows style guides
  3. Cover added functionality with unit tests and run PHPUnit to make sure, that all tests pass
  4. Update README.md to describe new or changed functionality
  5. Add changes description to CHANGES.md file. Use Semantic Versioning convention to determine next version number.
  6. When ready, create pull request

Make shortcuts

If you have GNU Make installed, you can use following shortcuts:

  • make cs (instead of php vendor/bin/phpcs) -
    run static code analysis with PHP_CodeSniffer
    to check code style
  • make csfix (instead of php vendor/bin/phpcbf) -
    fix code style violations with PHP_CodeSniffer
    automatically, where possible (ex. PSR-2 code formatting violations)
  • make test (instead of php vendor/bin/phpunit) -
    run tests with PHPUnit
  • make install - instead of composer install * make all or just make without parameters -
    invokes described above install, cs, test tasks sequentially -
    project will be assembled, checked with linter and tested with one single command

Resources

About

Controllers for common UI and endpoints in Laravel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 99.5%
  • Makefile 0.5%