Current SqueHub versionv2.0.0
Getting started Β· v1.x

Namespaces

πŸ—‚οΈ Namespaces & Usage in Squehub Namespaces in Squehub organize your code into logical modules and folders, following PSR-4 standards. They allow autoloading and help avoid class name conflicts. πŸ“¦ Core Directory Namespaces All core…

Historical v1.x reference. These examples describe the original API. Read the current v2.x guides or the upgrade guide.

πŸ—‚οΈ Namespaces & Usage in Squehub

Namespaces in Squehub organize your code into logical modules and folders, following PSR-4 standards. They allow autoloading and help avoid class name conflicts.

πŸ“¦ Core Directory Namespaces

All core framework classes live under the App\Core namespace. This includes foundational classes and utilities such as:

  • Helper – common utility functions (globally available, no import needed)
  • View – template rendering engine
  • Mail – email sending
  • Notification – app notifications
  • Verification – verification utilities
  • Model – base model class for database ORM
  • MiddlewareHandler – middleware pipeline manager
  • Task – for scheduled/background tasks
  • Dumpper – data seeding and dumping tool

πŸ“ Core Directory Structure Example

CODE
app/core/
β”œβ”€β”€ Helper.php
β”œβ”€β”€ Mail.php
β”œβ”€β”€ MiddlewareHandler.php
β”œβ”€β”€ Model.php
β”œβ”€β”€ Notification.php
β”œβ”€β”€ Task.php
β”œβ”€β”€ Dumpper.php
β”œβ”€β”€ Verification.php
β”œβ”€β”€ View.php
└── ...

πŸ“ Project Directory & Namespace Structure

Your application files live under the Project namespace. This mirrors the folder structure inside project/. For example:

CODE
project/
β”œβ”€β”€ controllers/
β”‚   └── AuthController.php
β”œβ”€β”€ models/
β”‚   └── User.php
β”œβ”€β”€ middleware/
β”‚   β”œβ”€β”€ AuthMiddleware.php
β”‚   └── auth/
β”‚       └── VerifyAdminMiddleware.php
β”œβ”€β”€ utils/                 # Custom utility/helper PHP files (loaded globally)
β”‚   └── customHelpers.php
└── tasks/
    └── CleanUpTask.php

🌟 Utils Directory: Global Utility Files

Both project/utils/ and any Utils/ directories inside packages may contain PHP files with helper functions or classes. These are automatically loaded globally by the framework during bootstrap, so you do not need to import them explicitly.

πŸ“œ Declaring Namespaces

In Core Files

Each core file must declare the App\Core namespace at the top (except helper functions which are globally accessible without import):

CODE
<?php
namespace App\Core;

class MiddlewareHandler {
    // ...
}

In Project Files

Declare namespaces matching the folder under Project. Examples:

CODE
<?php
namespace Project\Models;

use App\Core\Model;

class User extends Model {
    // ...
}
CODE
<?php
namespace Project\Middleware\Auth;

class VerifyAdminMiddleware {
    // ...
}
CODE
<?php
namespace Project\Tasks;

class CleanUpTask {
    // ...
}

πŸ“Œ Important Notes

  • The namespace must exactly match the folder structure, including case sensitivity.
  • For subfolders, namespaces are nested (e.g., Project\Middleware\Auth for project/middleware/auth/).
  • Composer autoloading depends on this matching to work properly.
  • Helper functions and utility files inside project/utils/ and package Utils/ folders are globally loaded; no import needed.

πŸ›  Importing & Using Core Classes in Project Code

Use use statements to import core classes before using them in your controllers, models, middleware, tasks, or routes β€” except for helpers and utils, which require no import:

CODE
use App\Core\View;
use App\Core\Mail;
use App\Core\Notification;
use App\Core\Verification;
use App\Core\Model;
use App\Core\MiddlewareHandler;
use App\Core\Task;
use App\Core\Dumpper;

// Helper and Utils functions (csrf_token(), route(), custom helpers, etc.) are globally available without importing.

πŸ–Ό Using Views in Routes or Controllers

The View class must be imported to render templates properly. Use dot notation matching the view folder structure:

CODE
use App\Core\View;

return View::render('admin.dashboard');  // loads views/admin/dashboard.squehub.php

Make sure your views folders and namespaces correspond exactly for Squehub’s view loader to find the files.

βœ… Summary

  • All core framework files reside in the App\Core namespace, except helper and utils functions that are globally available.
  • Your app files live in the Project namespace matching their folder structure.
  • Namespaces must be declared at the top of each PHP file.
  • Match namespace exactly to folder names (case-sensitive).
  • Use use to import classes before usage, except for helpers and utils.
  • Keep middleware, tasks, dumpper, and other components organized into subfolders with matching namespaces.
  • Views must be called with dot notation matching their folder paths and imported namespaces.
πŸ’‘ Tip: Proper namespace usage and folder structure organization keep your codebase clean, maintainable, and scalable as your project grows.