Routing is the foundation of your application. It maps URLs to controller methods, handles parameters, and applies middleware. The Mini Framework provides a Laravel-like routing system that's both powerful and easy to use.
Routes are defined in routes/web.php using the Route facade.
You can define GET and POST routes that map to controller methods.
// routes/web.php
use Core\Route;
// Simple route to homepage
Route::get('/', 'HomeController@index');
// Route to a specific page
Route::get('/about', 'PageController@about');
// Route to a controller method
Route::get('/users', 'UserController@index');
// Handle form submissions
Route::post('/users', 'UserController@store');
// Handle updates
Route::post('/users/{id}', 'UserController@update');
// Handle deletions
Route::post('/users/{id}/delete', 'UserController@delete');
You can capture URL segments as parameters using curly braces {}.
These parameters are automatically passed to your controller methods.
// Route definition
Route::get('/user/{id}', 'UserController@show');
// Controller method receives the parameter
// app/Controllers/UserController.php
public function show(Request $request, $id)
{
// $id contains the value from the URL
// Example: /user/123 → $id = '123'
$user = User::find($id);
return $this->view('user.show', ['user' => $user]);
}
// Route with multiple parameters
Route::get('/user/{userId}/post/{postId}', 'PostController@show');
// Controller method
public function show(Request $request, $userId, $postId)
{
// Parameters are passed in order
// /user/5/post/10 → $userId = '5', $postId = '10'
return $this->view('post.show', [
'userId' => $userId,
'postId' => $postId
]);
}
$request.
Route groups allow you to apply middleware or a prefix to multiple routes at once. This keeps your routes organized and DRY (Don't Repeat Yourself).
// Apply middleware to multiple routes
Route::group(['middleware' => 'auth'], function () {
Route::get('/profile', 'UserController@profile');
Route::get('/settings', 'UserController@settings');
Route::get('/dashboard', 'DashboardController@index');
});
// All routes in the group require authentication
// Add a prefix to all routes in the group
Route::group(['prefix' => '/api'], function () {
Route::get('/users', 'ApiController@users');
Route::get('/products', 'ApiController@products');
});
// Results in: /api/users, /api/products
// Combine prefix and middleware
Route::group(['prefix' => '/admin', 'middleware' => 'admin'], function () {
Route::get('/users', 'AdminController@users');
Route::get('/settings', 'AdminController@settings');
});
// Results in: /admin/users, /admin/settings
// All routes require 'admin' middleware
You can apply middleware to individual routes using the chainable middleware() method.
// Apply middleware to a single route
Route::middleware('auth')
->get('/dashboard', 'DashboardController@index');
// Multiple middleware
Route::middleware(['auth', 'admin'])
->get('/admin', 'AdminController@index');
// Chainable syntax
Route::middleware('auth')
->get('/profile', 'UserController@profile');
middleware() for single routes that need specific protection.
The framework provides two ways to define routes. The Route facade is the recommended
approach for a Laravel-like experience.
// routes/web.php
use Core\Route;
Route::get('/', 'HomeController@index');
Route::post('/users', 'UserController@store');
Route::middleware('auth')->get('/dashboard', 'DashboardController@index');
// routes/web.php (old way, still works)
$router->get('/', 'HomeController@index');
$router->post('/users', 'UserController@store');
// Not recommended - use Route facade instead
$router variable still works,
the Route facade is the preferred method as it provides better syntax and
is more consistent with Laravel conventions.
Use the CLI command to see all registered routes in your application.
php mini route:list
This displays a table showing:
// routes/web.php
use Core\Route;
// Public routes
Route::get('/', 'HomeController@index');
Route::get('/about', 'PageController@about');
Route::get('/contact', 'PageController@contact');
// User routes with parameters
Route::get('/user/{id}', 'UserController@show');
Route::get('/user/{id}/posts', 'UserController@posts');
// Protected routes (require authentication)
Route::group(['middleware' => 'auth'], function () {
Route::get('/dashboard', 'DashboardController@index');
Route::get('/profile', 'UserController@profile');
Route::get('/settings', 'UserController@settings');
});
// Admin routes (require admin middleware)
Route::group(['prefix' => '/admin', 'middleware' => 'admin'], function () {
Route::get('/users', 'AdminController@users');
Route::get('/products', 'AdminController@products');
});
// API routes
Route::group(['prefix' => '/api'], function () {
Route::get('/users', 'ApiController@users');
Route::post('/users', 'ApiController@createUser');
});
// Single route with middleware
Route::middleware('auth')->get('/premium', 'ContentController@premium');
Now that you understand routing, learn how to: