Installation:
composer require frodosghost/restful
Add the service provider to config/app.php:
'providers' => [
// ...
FrodosGhost\Restful\RestfulServiceProvider::class,
],
Basic Route Definition:
Define RESTful routes in routes/api.php:
$restful = new \FrodosGhost\Restful\Restful('users');
$restful->get('index', 'UserController@index');
$restful->post('store', 'UserController@store');
$restful->get('show', 'UserController@show');
$restful->put('update', 'UserController@update');
$restful->delete('destroy', 'UserController@destroy');
First Use Case:
UserController) with standard CRUD methods./api/users (GET), /api/users (POST), etc.Resource-Based Routing:
Group routes by resource (e.g., users, posts) for logical separation.
$restful = new \FrodosGhost\Restful\Restful('posts');
$restful->get('index', 'PostController@index');
Custom Actions: Add non-standard HTTP methods or custom endpoints:
$restful->get('custom', 'UserController@customAction');
$restful->patch('partial-update', 'UserController@partialUpdate');
Middleware Integration: Attach middleware to all routes of a resource:
$restful = new \FrodosGhost\Restful\Restful('admin/users');
$restful->middleware('auth:admin');
Dependency Injection: Pass dependencies (e.g., repositories) to controllers via constructor binding in Laravel’s IoC container.
Laravel Compatibility:
Works seamlessly with Laravel’s routing, middleware, and service container. Use Laravel’s Route::prefix() or Route::namespace() alongside Restful for deeper integration.
Route::prefix('api/v1')->group(function () {
$restful = new \FrodosGhost\Restful\Restful('v1/users');
// ...
});
API Versioning:
Combine with Laravel’s route model binding or versioning strategies (e.g., /v1/users).
Validation:
Use Laravel’s Form Request validation in controllers (e.g., StoreUserRequest) for consistent validation logic.
Namespace Collisions:
Avoid naming conflicts with Laravel’s built-in routes. Prefix custom routes or resources (e.g., api/v1/custom-resource).
Method Overrides:
Ensure controller methods match the HTTP verbs (e.g., update for PUT/PATCH). Misalignment can cause 404 errors.
Resource Naming:
Hyphens (-) in resource names (e.g., user-profiles) may cause issues. Use underscores (user_profiles) or camelCase (userProfiles) instead.
Middleware Order:
Middleware attached to Restful routes runs after Laravel’s global middleware. Use Route::middleware() for priority adjustments.
Route Debugging: Dump registered routes to verify setup:
php artisan route:list
Look for GET|POST|PUT|DELETE /api/{resource} patterns.
Controller Debugging:
Add dd($request->all()) in controller methods to inspect incoming data.
Custom Response Formatting: Override the default JSON response in controllers or create a base controller:
class BaseApiController extends Controller {
protected function respond($data, $status = 200) {
return response()->json($data, $status);
}
}
Dynamic Resource Binding:
Extend the Restful class to support dynamic resource names or query parameters:
$restful = new \FrodosGhost\Restful\Restful(request('resource'));
Authentication: Integrate with Laravel’s auth (e.g., Sanctum, Passport) by attaching middleware:
$restful->middleware('auth:sanctum');
Rate Limiting:
Apply Laravel’s throttling middleware to Restful routes:
$restful->middleware('throttle:60,1');
How can I help you explore Laravel packages today?