π¦ 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.
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.
/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 resourcesPOST
β for creating data or submitting formsPUT
β for updating existing dataDELETE
β 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 routesroutes/admin/dashboard.php
β admin dashboard and statsroutes/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
/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.
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
use App\Core\View;
and use Project\Models\User;
inside route files
to access views, models, middleware, and packages.