Download the PHP package paigejulianne/nanomvc without Composer
On this page you can find all versions of the php package paigejulianne/nanomvc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download paigejulianne/nanomvc
More information about paigejulianne/nanomvc
Files in paigejulianne/nanomvc
Package nanomvc
Short Description NanoMVC: a lightweight MVC framework with Blade, Smarty, and Twig template support
License GPL-3.0-or-later
Homepage https://github.com/paigejulianne/nanomvc
Informations about the package nanomvc
NanoMVC
A lightweight MVC framework for PHP 8.0+ with support for Blade, Smarty, and Twig templates.
Version 1.0.1 | License: GPL-3.0
by Paige Julianne Sullivan paigejulianne.com | GitHub
Features
- Enterprise-scale performance: O(1) route lookup, route caching, middleware caching
- Multiple template engines: PHP, Blade, Smarty, and Twig support
- Simple routing: Clean URL routing with parameters and groups
- Zero dependencies: Only requires PHP 8.0+ (template engines optional)
- Built-in validation: Request validation with helpful error messages
- Session management: Secure sessions with CSRF protection
- Rate limiting: Configurable throttling for API endpoints
- Response compression: Automatic gzip compression
- Integrates with NanoORM: Seamlessly works with NanoORM for database operations
Prerequisites
- PHP 8.0+ with the following extensions:
json(usually enabled by default)mbstring(recommended)
- Apache with
mod_rewriteenabled, or Nginx - Composer (for installation and autoloading)
Installation
Via Composer (Recommended)
Manual Installation
Download NanoMVC.php and include it in your project:
Examples
NanoMVC includes four example applications demonstrating each template engine:
| Example | Template Engine | Directory |
|---|---|---|
| PHP (Native) | Native PHP | example/ |
| Blade | Laravel Blade | example-blade/ |
| Smarty | Smarty | example-smarty/ |
| Twig | Twig | example-twig/ |
Running the Examples
-
Install dependencies:
-
For Blade example, also install:
-
For Smarty example, also install:
-
For Twig example, also install:
-
Set file permissions:
- Access in your browser:
- PHP:
http://localhost/path/to/nanomvc/example/ - Blade:
http://localhost/path/to/nanomvc/example-blade/ - Smarty:
http://localhost/path/to/nanomvc/example-smarty/ - Twig:
http://localhost/path/to/nanomvc/example-twig/
- PHP:
Note: Ensure Apache
mod_rewriteis enabled andAllowOverride Allis set for your directory. See Apache Configuration for details.
Quick Start
1. Create Your Entry Point
Create index.php:
2. Create Configuration (Optional)
Create .config:
3. Define Routes
Create routes.php:
4. Create a Controller
Create controllers/HomeController.php:
5. Create a View
Create views/home.php:
Configuration
Using a .config File
Programmatic Configuration
Routing
Basic Routes
Route Parameters
Route Groups
Nested Groups
Route Handlers
Routes can use closures or controller methods:
Middleware
Custom Error Handlers
Route Caching (Production)
For large applications, cache compiled routes for faster startup:
Route Statistics
Monitor route performance:
Controllers
Creating Controllers
Extend the base Controller class:
Response Methods
Validation
Available Rules:
required- Field must be present and not emptyemail- Must be a valid email addressnumeric- Must be numericinteger- Must be an integermin:n- Minimum string lengthmax:n- Maximum string lengthin:a,b,c- Must be one of the specified valuesurl- Must be a valid URLalpha- Must contain only lettersalphanumeric- Must contain only letters and numbers
Request Object
Getting Input
Route Parameters
Request Info
Response Object
Creating Responses
Modifying Responses
Response Compression
Responses are automatically gzip-compressed when the client supports it:
Views
Template Engines
PHP (Native) - Default, no dependencies:
Blade - Requires jenssegers/blade:
Smarty - Requires smarty/smarty:
Twig - Requires twig/twig:
Rendering Views
Shared Data
Template Examples
PHP Template (views/users/index.php):
Blade Template (views/users/index.blade.php):
Smarty Template (views/users/index.tpl):
Twig Template (views/users/index.twig):
Integration with NanoORM
Creating Models
Using Models in Controllers
Session Management
Basic Usage
Flash Messages
CSRF Protection
Session Configuration
Custom Session Storage
Rate Limiting
Basic Usage
Throttle Middleware
Security Middleware
CSRF Middleware
CORS Middleware
Directory Structure
Apache Configuration
Basic .htaccess
Create .htaccess in your application directory:
Note: Do not use
RewriteBaseunless your application is installed at the web root. The above configuration works for subdirectory installations.
File Permissions
Ensure Apache can read the .htaccess file:
Enabling mod_rewrite
If mod_rewrite is not enabled, run:
Apache User Directories (~username)
If running NanoMVC in a user directory (e.g., http://localhost/~username/myapp/), you need to configure Apache to allow .htaccess overrides.
Edit /etc/apache2/mods-available/userdir.conf:
Then restart Apache:
Subdirectory Installation
NanoMVC automatically detects when installed in a subdirectory and adjusts routing accordingly. For links in your views, calculate the base URL:
Nginx Configuration
Testing
Run tests with PHPUnit:
API Reference
Router
| Method | Description |
|---|---|
get($path, $handler, $middleware) |
Register GET route |
post($path, $handler, $middleware) |
Register POST route |
put($path, $handler, $middleware) |
Register PUT route |
patch($path, $handler, $middleware) |
Register PATCH route |
delete($path, $handler, $middleware) |
Register DELETE route |
any($path, $handler, $middleware) |
Register route for all methods |
match($methods, $path, $handler, $middleware) |
Register route for specific methods |
group($options, $callback) |
Create route group |
dispatch($request) |
Dispatch request to handler |
setNotFoundHandler($handler) |
Set 404 handler |
setErrorHandler($handler) |
Set error handler |
Controller
| Method | Description |
|---|---|
view($template, $data, $status) |
Render view response |
json($data, $status) |
JSON response |
redirect($url, $status) |
Redirect response |
text($content, $status) |
Plain text response |
html($content, $status) |
HTML response |
validate($rules) |
Validate request input |
request() |
Get current request |
Request
| Method | Description |
|---|---|
method() |
Get HTTP method |
path() |
Get request path |
query($key, $default) |
Get query parameter |
input($key, $default) |
Get input value |
all() |
Get all input |
only($keys) |
Get specific keys |
except($keys) |
Get all except keys |
has($key) |
Check if key exists |
param($name, $default) |
Get route parameter |
params() |
Get all route parameters |
header($name, $default) |
Get header |
cookie($name, $default) |
Get cookie |
isAjax() |
Check if AJAX request |
expectsJson() |
Check if expects JSON |
json() |
Get JSON body |
Response
| Method | Description |
|---|---|
setContent($content) |
Set response body |
setStatusCode($code) |
Set HTTP status |
header($name, $value) |
Add header |
withHeaders($headers) |
Add multiple headers |
send() |
Send response |
json($data, $status) |
Create JSON response |
redirect($url, $status) |
Create redirect response |
text($content, $status) |
Create text response |
html($content, $status) |
Create HTML response |
View
| Method | Description |
|---|---|
configure($viewsPath, $cachePath, $engine) |
Configure view system |
render($template, $data) |
Render template to string |
make($template, $data, $status) |
Create response with view |
share($key, $value) |
Share data with all views |
engineAvailable($engine) |
Check if engine available |
App
| Method | Description |
|---|---|
run($basePath) |
Run the application |
config($key, $default) |
Get config value |
setConfig($key, $value) |
Set config value |
isDebug() |
Check debug mode |
setDebug($debug) |
Set debug mode |
basePath($path) |
Get base path |
Session
| Method | Description |
|---|---|
start() |
Start the session |
get($key, $default) |
Get session value |
set($key, $value) |
Set session value |
has($key) |
Check if key exists |
forget($key) |
Remove session value |
all() |
Get all session data |
flush() |
Clear all session data |
destroy() |
Destroy session completely |
flash($key, $value) |
Flash value for next request |
getFlash($key, $default) |
Get and remove flash value |
csrfToken() |
Get CSRF token |
verifyCsrfToken($token) |
Verify CSRF token |
configure($config) |
Configure session settings |
setDriver($driver) |
Set custom session driver |
regenerate() |
Regenerate session ID |
RateLimiter
| Method | Description |
|---|---|
attempt($key, $max, $decay) |
Check/increment rate limit |
remaining($key, $max) |
Get remaining attempts |
availableIn($key) |
Seconds until limit resets |
clear($key) |
Clear rate limit for key |
hits($key) |
Get current hit count |
setStore($store) |
Set storage backend |
Router (Additional Methods)
| Method | Description |
|---|---|
cacheRoutes($file) |
Cache routes to file |
loadCachedRoutes($file) |
Load routes from cache |
isRouteCached() |
Check if routes are cached |
getStats() |
Get route statistics |
clearMiddlewareCache() |
Clear middleware cache |
Response (Additional Methods)
| Method | Description |
|---|---|
withCompression() |
Enable gzip compression |
withoutCompression() |
Disable gzip compression |
configureCompression($threshold, $level) |
Configure compression settings |
Request (Additional Methods)
| Method | Description |
|---|---|
getContentStream() |
Get stream handle for body |
readContentChunked($callback, $size) |
Read body in chunks |
setMaxBodySize($bytes) |
Set max request body size |
getMaxBodySize() |
Get max request body size |
Troubleshooting
404 Errors on All Routes
-
Check mod_rewrite is enabled:
-
Check AllowOverride is set: Ensure your Apache configuration allows
.htaccessfiles. See Apache User Directories above. - Check .htaccess permissions:
403 Forbidden / "Unable to read htaccess file"
This is a file permissions issue. Apache cannot read the .htaccess file:
500 Internal Server Error
-
Check PHP error logs: Usually at
/var/log/apache2/error.log -
Enable debug mode: Set
debug=truein your.configfile - Check .htaccess syntax: Some directives (like
Options) may not be allowed in your Apache configuration
Views Not Loading
Ensure view paths in .config are relative to your application directory:
NanoMVC automatically resolves relative paths against the application's base directory.
Links Not Working in Subdirectory
When installed in a subdirectory, use $baseUrl for all links:
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Submit a pull request
License
NanoMVC is released under the GPL-3.0-or-later license.
Copyright 2024-present Paige Julianne Sullivan