Routing

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.

Basic Routing

Routes are defined in routes/web.php using the Route facade. You can define GET and POST routes that map to controller methods.

GET Routes

// 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');

POST Routes

// 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');

Route Parameters

You can capture URL segments as parameters using curly braces {}. These parameters are automatically passed to your controller methods.

Single Parameter

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

Multiple Parameters

// 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
    ]);
}
💡 Tip: Route parameters are passed to controller methods in the order they appear in the URL. The first parameter in the route is the first argument after $request.

Route Groups

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).

Group with Middleware

// 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

Group with Prefix

// 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

Group with Both

// 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

Middleware on Single Routes

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');
✅ Best Practice: Use route groups for multiple routes that share middleware. Use middleware() for single routes that need specific protection.

Route Facade vs Internal Router

The framework provides two ways to define routes. The Route facade is the recommended approach for a Laravel-like experience.

Using Route Facade (Recommended)

// routes/web.php
use Core\Route;

Route::get('/', 'HomeController@index');
Route::post('/users', 'UserController@store');
Route::middleware('auth')->get('/dashboard', 'DashboardController@index');

Using Internal Router (Legacy)

// routes/web.php (old way, still works)
$router->get('/', 'HomeController@index');
$router->post('/users', 'UserController@store');

// Not recommended - use Route facade instead
⚠️ Note: While the internal $router variable still works, the Route facade is the preferred method as it provides better syntax and is more consistent with Laravel conventions.

Viewing All Routes

Use the CLI command to see all registered routes in your application.

php mini route:list

This displays a table showing:

  • METHOD - HTTP method (GET, POST, etc.)
  • URI - Route path
  • ACTION - Controller@method or closure
  • MIDDLEWARE - Applied middleware

Complete Example

// 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');

Next Steps

Now that you understand routing, learn how to: