Package Data | |
---|---|
Maintainer Username: | saritasa |
Maintainer Contact: | sergey@saritasa.com (Sergey Populov) |
Package Create Date: | 2017-04-07 |
Package Last Update: | 2024-01-12 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-05-10 15:18:15 |
Package Statistics | |
---|---|
Total Downloads: | 12,699 |
Monthly Downloads: | 75 |
Daily Downloads: | 3 |
Total Stars: | 7 |
Total Watchers: | 3 |
Total Forks: | 4 |
Total Open Issues: | 0 |
Controllers for common UI and endpoints in Laravel,
like API authentication, password change, login page, etc.
Install the saritasa/laravel-controllers
package:
There are 2 types of controllers:
Controllers, described below, exist, but you must register routes for them manually
Recommended to use as base controller for other API controllers.
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
});
Controller for typical CRUD operations when you don't need many additional logic.
For using this functionality you can just create controller and set needed model class in property $modelClass
Example:
class CustomApiController extends ResourceApiController
{
/**
* Define model class that is managed by this controller.
*
* @var string
*/
protected $modelClass = App\Models\User::class;
}
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');});
$registrar->get('users', ApiController::class, 'list');
In this case ApiController::list will be calling with default dependency injection.
// Controller example
class ApiController {
public function show(int $id) {}
}
// Route example
$registrar->get('users/{id}', ApiController::class, 'show');
In this case ApiController::show will receive directly parameter from url. Ex: /user/5
// Controller example
class ApiController {
public function show(User $user) {}
}
// Route example
$registrar->get('users/{user}', ApiController::class, 'show');
In this case ApiController::show try to find Model user by id and if not exists throws ModelNotFoundException.
// Controller example
class ApiController {
public function show(Model $user) {}
}
// Route example
$registrar->get('users/{user}', ApiController::class, 'show', null, ['user' => User::class]);
In this case no matter what type hint in controller, container will be trying to give object of class that you pass in router registrar. So if method has type hinting of class which not a parent for given in router, TypeError will be thrown.
will be ignored.
If you have GNU Make installed, you can use following shortcuts:
make cs
(instead of php vendor/bin/phpcs
) -make csfix
(instead of php vendor/bin/phpcbf
) -make test
(instead of php vendor/bin/phpunit
) -make install
- instead of composer install
* make all
or just make
without parameters -