Left Icon Right Icon
Squehub SqueHub

🚦 Routing in Squehub

Squehub uses a simple yet powerful routing system that maps HTTP requests to controllers or closures. While /app/routes/web.php contains core framework-level or internal routes, developers are encouraged to define all custom application routes inside /project/routes/ to keep their code organized, upgrade-safe, and separated from the framework’s internals.

For example, you can create a dedicated route file like /project/routes/frontend.php to hold all frontend-related routes such as home, about, contact, and blog pages. This promotes better organization in larger projects.

πŸ’‘ Tip: You can split your routes further into subfolders such as routes/admin/dashboard.php or routes/api/v1.php for scalable routing structures.

API routes do not need to begin with /api/. However, it's highly recommended to use a recognizable pattern like /v1/users, /json/posts, or /api/products to indicate that the route is meant for API consumption. Structuring your route files under folders like routes/api/ also keeps things tidy and maintainable.

All route files and nested subdirectories inside /project/routes/ are automatically loaded by the framework. This approach makes it easier to structure routes for large or modular applications.

⚠️ Avoid editing /app/routes/web.php unless you're extending core behavior. Always register your own routes in /project/routes/ for better maintainability and clarity.

πŸ“Œ Basic Route

You can define a route using a closure and return a view directly:

Without importing App\Core\View, the framework will throw a "Class 'View' not found" error.

$router->add('GET', '/', function () {
    return View::render('home.welcome');
});

Note: The View::render('home.welcome') will look for the file views/home/welcome.squehub.php. The View class must be imported using:

use App\Core\View;

🧠 Routing to Controller Method

$router->add('GET', '/users', 'UserController@index');
$router->add('POST', '/user/store', 'UserController@store');

🧩 Dynamic Parameters

$router->add('GET', '/user/{id}', function($id) {
    return "User ID is: $id";
});

🌐 Supported HTTP Methods

  • GET – for displaying resources
  • POST – for creating data or submitting forms
  • PUT – for updating existing data
  • DELETE – for deleting resources
$router->add('GET', '/user/profile', 'UserController@show');
$router->add('POST', '/user/store', 'UserController@store');
$router->add('PUT', '/user/update/{id}', 'UserController@update');
$router->add('DELETE', '/user/delete/{id}', 'UserController@destroy');

🏷 Named Routes

$router->add('GET', '/login', 'AuthController@login', 'login.page');

In Blade-like views:

<a href="#">Login</a>

πŸ›‘ Middleware Support

$router->add(
  'GET',
  '/register',
  'AuthController@showRegistrationForm',
  'register.form',
  [RedirectIfAuthenticatedMiddleware::class]
);

πŸ—‚ Route Groups

$router->group(['prefix' => '/admin', 'middleware' => [AuthMiddleware::class]], function($router) {
    $router->add('GET', '/dashboard', 'AdminController@dashboard');
    $router->add('GET', '/users', 'AdminController@users');
});

πŸ“ Loading Routes from Files

  • routes/frontend.php – frontend-facing routes
  • routes/admin/dashboard.php – admin dashboard and stats
  • routes/api/v1.php – API endpoints (can be RESTful)

πŸ“¦ Example Route Setup

$router->add('GET', '/', function () {
    return View::render('home.welcome');
});

$router->add('GET', '/about', function () {
    return View::render('home.about');
});

$router->add('GET', '/users', 'UserController@all');
$router->add('GET', '/user/{id}', 'UserController@show');
$router->add('POST', '/user/create', 'UserController@create');

πŸ”Œ API Routing

πŸ’‘ Tip: API routes can be defined using any URL pattern. While they don't need to start with /api/, using identifiable prefixes like /api/v1/, /json/, or /v1/ helps separate frontend and backend logic.

Closure-based API Routes

$router->add('GET', '/v1/user', function () {
    return json_encode(['id' => 1, 'name' => 'John Doe']);
});

$router->add('POST', '/json/data', function () {
    $input = json_decode(file_get_contents('php://input'), true);
    return json_encode(['received' => $input]);
});

Controller-based REST API

$router->add('GET', '/api/users', 'UsersController@index');
$router->add('GET', '/api/users/{id}', 'UsersController@show');
$router->add('POST', '/api/users', 'UsersController@store');
$router->add('PUT', '/api/users/{id}', 'UsersController@update');
$router->add('DELETE', '/api/users/{id}', 'UsersController@destroy');

Example Controller Method

public function update($id) {
    $data = json_decode(file_get_contents('php://input'), true);
    return json_encode(User::update($id, $data));
}

🚧 Error Handling

$router->setNotFoundHandler(function() {
    return View::render('errors.404');
});

πŸ“š Using Namespaces in Routes

<?php
use App\Core\View;
use Project\Models\User;

$router->add('GET', '/user/{id}', function($id) {
    return json_encode(User::find($id));
});

πŸ“ View Loading Behavior

Views in Squehub are rendered using the View::render('path.to.view') method. This allows developers to display templates in a clean and modular way.

  • View::render('home.welcome') β†’ views/home/welcome.squehub.php
  • View::render('admin.dashboard') β†’ views/admin/dashboard.squehub.php

⚠️ If you're rendering a view inside a closure route, you must import the View class manually:

<?php
use App\Core\View;

$router->add('GET', '/', function () {
    return View::render('home.welcome');
});

Without importing App\Core\View, the framework will throw a "Class 'View' not found" error.

Alternatively, you can use the fully qualified class name directly in the route if you prefer not to import:

$router->add('GET', '/', function () {
    return \App\Core\View::render('home.welcome');
});

Squehub’s view system automatically maps dot-notation paths to files inside the views/ directory using the .squehub.php extension.

πŸ’‘ Tip: Always make sure the corresponding view directory and file exists (e.g. views/home/welcome.squehub.php), otherwise the system will throw a "view not found" error.

πŸš€ Recap

  • Define routes with $router->add(method, path, handler, name?, middleware?)
  • Use closures or Controller@method
  • Apply middleware directly or via route groups
  • Use route names inside templates
  • Store routes in /project/routes/
  • Use all HTTP verbs: GET, POST, PUT, DELETE
  • Closures can return JSON for APIs using json_encode()
  • Import App\Core\View when rendering views in closures
  • Views are loaded from views/ using dot notation and .squehub.php files
πŸ’‘ Tip: You can use namespaces like use App\Core\View; and use Project\Models\User; inside route files to access views, models, middleware, and packages.
;