Download the PHP package spinalcord/square-routing without Composer
On this page you can find all versions of the php package spinalcord/square-routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spinalcord/square-routing
More information about spinalcord/square-routing
Files in spinalcord/square-routing
Package square-routing
Short Description simple yet powerfull routing script
License MIT
Informations about the package square-routing
SquareRouting
SquareRouting (Approx 0.35 Mb without comments) is a powerful, fast, and flexible PHP MVC micro-framework designed to provide a robust foundation for building web applications. It emphasizes clean URL structures, efficient routing, and includes essential features like rate limiting, caching, and a built-in account system. Leave a ⭐if you like this project.
Table of Contents
- Features
- Installation
- Usage
- Core Concepts
- Defining Routes
- Route Collection
- Controllers
- Route Filters (This idea is from Lightpack)
- Built-in Features
- CORS Protection
- Caching
- Rate Limiting
- Environment Variables (.env)
- Input Validation
- Database Operations
- Account System
- ORM-like DB Table Generation
- Template Engine (Views)
- Language Support
- Configuration System
- Response Handling
- HTML Page Example
- Redirection Example
- Core Concepts
- Running the Application
- License
Features
- Language Support: Multi-language support via route segments (e.g. http://localhost:8000/en)
- Flexible Routing: Define routes for GET, POST, PUT, DELETE, PATCH, and REROUTE (redirection) methods.
- Validator-Based Route Parameters: Modern approach using validator objects instead of regex patterns for type-safe route parameter validation. Includes built-in validators for numbers, strings, alphanumeric, and path parameters with slashes.
- Route Filters: Apply 'before' and 'after' filters to routes for tasks like authentication, logging, or data manipulation. Filters are classes with
beforeandaftermethods that receive theDependencyContainer. - Dependency Injection: Integrates with a
DependencyContainerfor managing and injecting dependencies into controllers and filters. - Built-in Rate Limiting: Protect your endpoints from abuse with an easy-to-use rate limiting mechanism that stores data in a JSON file.
- Built-in Caching Mechanism: Implement basic caching for frequently accessed data to improve performance.
- Flexible Response Handling: Convenient methods for sending JSON, HTML, or performing redirects.
- CORS Protection: Easily configure Cross-Origin Resource Sharing to control access to your resources.
- Input Validation: Robust validation rules for various data types, including nested and array validation.
- Custom Database Class: A handy way to modify your database.
- Template Engine: Simple PHP-based template engine for rendering dynamic HTML views with built-in caching.
- No
static!: You heard it right! Nostaticis used in this project, which makes this project perfectly fine for testing.Installation
This project uses Composer for dependency management.
-
Clone the repository:
- Or with Composer:
Usage
Core Concepts
Defining Routes
Create your routes in backend/Routes/ApplicationRoutes.php. Here's how to add different types of routes:
Validator-Based Route Parameters
SquareRouting uses modern validator objects instead of regex patterns for route parameter validation. This provides better type safety and reusability.
Available Built-in Validators:
IsNumber- Validates numeric values (integers, floats)IsString- Validates string valuesAlphaNumeric- Validates alphanumeric strings (letters + numbers only)IsPath- Validates path parameters with slashes (for blog posts, file paths, etc.)Required- Ensures parameter is present and not emptyEmail- Validates email formatMin- Validates minimum length/valueMax- Validates maximum length/value
Usage Examples:
Creating Custom Validators:
Benefits over Regex Patterns:
- Type Safety - No more string-based pattern errors
- Reusable - Same validators for forms and routes
- Extensible - Easy to create custom validation logic
- Better Error Messages - Clear validation feedback
- IDE Support - Autocomplete and type hinting
Route Collection
Use RouteCollector to combine routes from different sources. This is useful for modular applications or plugin systems:
Controllers
Controllers handle your application logic. They get the DependencyContainer automatically:
Route Filters
Filters let you run code before and after your routes. Great for authentication or logging:
Apply filters to your routes:
Built-in Features
CORS Protection
Set allowed domains in your .env file:
Leave empty to allow all origins.
Caching
Cache expensive operations easily:
Rate Limiting
Protect your API from abuse:
Environment Variables (.env)
Access your environment variables:
Input Validation
Validate user input with simple rules:
Database Operations
Simple database operations. Ensures compatiblity with sqlite and mysql.
Account System
Built-in user authentication (Needs a database connection, use sqlite in the .env if you don't have mysql ready):
ORM-like Table Generation
Define your database schema with PHP objects, ensures compatiblity between Sqlite and Mysql:
Template Engine (Views)
Render HTML templates with a Twig-like syntax:
Template file (backend/Templates/homepage.tpl):
Language Support
-
Support multiple languages with automatic detection (doesn't affect the defined routepath!):
-
Set default language in
.env: - Or let the user decide what his language is:
then do $this->language->setLanguage($lang, true); in your controller. Done.
- Load the language string from a json like this:
- As you cann see my language system supports nested definitions.
- Also parameters are supported.
and load it like this in php:
Configuration System
Manage super fast cached application settings in your database easily (Use sqlite in the .env if you don't have mysql ready):
Response Handling
HTML Page Example
Return simple HTML pages:
Redirection Example
Redirect users to other pages:
Run the Server
Start the built-in PHP server:
Then visit http://localhost:8000 in your browser.
License
This project is licensed under the MIT License - see the LICENSE file for details.