Routing
Build clean and maintainable URL structures with Yabasi's powerful routing system
Basic Routing
The most basic Yabasi routes accept a URI and a closure. Define your routes in routes/web.php
:
use Yabasi\Routing\Router;
$router->get('/greeting', function () {
return 'Hello, World!';
});
$router->get('/users/{id}', function ($id) {
return "User ID: " . $id;
});
Route parameters are automatically injected into your route callbacks.
HTTP Methods
Available Router Methods
Yabasi router provides methods for all common HTTP verbs. Each method accepts a URI as first argument and a callback as second argument:
// Basic route definitions
$router->get('/users', function () {
return 'GET request to /users';
});
$router->post('/users', function () {
return 'POST request to /users';
});
$router->put('/users/{id}', function ($id) {
return "PUT request to /users/$id";
});
$router->patch('/users/{id}', function ($id) {
return "PATCH request to /users/$id";
});
$router->delete('/users/{id}', function ($id) {
return "DELETE request to /users/$id";
});
HTTP Methods Quick Reference
Controller Method Routing
Instead of using closures, you can route to controller methods using the ControllerName@methodName syntax:
// UserController routes
$router->get('/users', 'UserController@index');
$router->post('/users', 'UserController@store');
$router->get('/users/{id}', 'UserController@show');
$router->put('/users/{id}', 'UserController@update');
$router->delete('/users/{id}', 'UserController@destroy');
Controller methods receive route parameters in the same order they appear in the URL.
Route Groups
Group routes to share common attributes like middleware or URL prefixes:
$router->group(['middleware' => ['auth']], function ($router) {
$router->get('/dashboard', 'DashboardController@index');
$router->get('/profile', 'ProfileController@show');
// Nested group with prefix
$router->group(['prefix' => 'admin'], function ($router) {
$router->get('/settings', 'AdminController@settings');
$router->get('/users', 'AdminController@users');
});
});
Middleware Groups
Apply middleware to multiple routes at once
URL Prefixes
Group routes under a common URL segment
Route Parameters
Capture URL segments to pass data to your route handlers:
// Required parameters
$router->get('/users/{id}', function ($id) {
return "User ID: " . $id;
});
// Multiple parameters
$router->get('/posts/{post}/comments/{comment}', function ($postId, $commentId) {
return "Post: $postId, Comment: $commentId";
});
Parameter names should be unique within a route definition.
Form Method Spoofing
HTML forms don't support PUT, PATCH, or DELETE methods directly. Use method spoofing to work around this limitation:
<form action="/users/1" method="POST">
<input type="hidden" name="_method" value="PUT">
{{ csrf_field()|raw }}
<!-- Form fields -->
</form>
The _method
field will be detected and used as the HTTP method.
CSRF Protection
Yabasi automatically protects your application against Cross-Site Request Forgery (CSRF) attacks:
<form method="POST" action="/profile">
{{ csrf_field()|raw }}
<!-- or -->
<input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
<!-- Form fields -->
</form>
Required for POST Requests
All POST requests must include a valid CSRF token
Automatic Verification
Tokens are automatically verified on each request
Next Steps
Now that you understand routing, explore these related topics: