mirror of
https://github.com/WilliamAAK/Route2.git
synced 2025-04-19 16:17:18 +00:00
Compare commits
3 Commits
1c55782058
...
c413451500
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c413451500 | ||
![]() |
f9b451ef48 | ||
![]() |
25ad36a9aa |
81
README.md
81
README.md
@ -15,12 +15,25 @@ Route::get('/home', function () {
|
|||||||
echo "Welcome to the homepage!";
|
echo "Welcome to the homepage!";
|
||||||
});
|
});
|
||||||
|
|
||||||
// Define a middleware
|
// Define a route with a parameter
|
||||||
|
Route::get('/user/$id', function ($id) {
|
||||||
|
echo "User ID: $id";
|
||||||
|
});
|
||||||
|
|
||||||
|
// Define a route with an optional parameter
|
||||||
|
Route::get('/user/$id?', function ($id = null) {
|
||||||
|
echo $id ? "User ID: $id" : "No User ID provided.";
|
||||||
|
});
|
||||||
|
|
||||||
|
// Defining middlewares
|
||||||
Route::before(function () {
|
Route::before(function () {
|
||||||
echo "Middleware executed before the route callback.";
|
echo "Middleware executed before the route callback.";
|
||||||
});
|
});
|
||||||
|
Route::after(function () {
|
||||||
|
echo "Middleware executed after the route callback.";
|
||||||
|
});
|
||||||
|
|
||||||
// Define a POST route with middleware
|
// Define a route with a middleware attached to it
|
||||||
Route::post('/submit', function () {
|
Route::post('/submit', function () {
|
||||||
echo "Form submitted!";
|
echo "Form submitted!";
|
||||||
}, function () {
|
}, function () {
|
||||||
@ -29,7 +42,7 @@ Route::post('/submit', function () {
|
|||||||
|
|
||||||
// Group routes under a common prefix
|
// Group routes under a common prefix
|
||||||
Route::group('/admin', function () {
|
Route::group('/admin', function () {
|
||||||
// Define a middleware for the group; it will not affect the outer routes
|
// Middlewares defined here will not affect the routes outside this group
|
||||||
Route::before(function () {
|
Route::before(function () {
|
||||||
echo "Admin Middleware executed.";
|
echo "Admin Middleware executed.";
|
||||||
});
|
});
|
||||||
@ -44,10 +57,10 @@ Route::group('/admin', function () {
|
|||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
## Composer
|
### Composer:
|
||||||
|
|
||||||
Composer needs to know where to locate the package. Add the repository to your project by declaring it in the `composer.json` file.
|
Composer needs to know where to locate the package. Add the repository to your project by declaring it in the `composer.json` file.
|
||||||
```JSON
|
```json
|
||||||
{
|
{
|
||||||
"repositories": [
|
"repositories": [
|
||||||
{
|
{
|
||||||
@ -64,44 +77,62 @@ Fetch the package by running
|
|||||||
|
|
||||||
composer install
|
composer install
|
||||||
|
|
||||||
## Manual
|
### Manual:
|
||||||
|
|
||||||
Its all in a single file; include it in your project like so
|
Its all in a single file; include it in your project like so.
|
||||||
|
|
||||||
```PHP
|
```php
|
||||||
require '/YourPath/Route.php'
|
require '/YourPath/Route.php'
|
||||||
```
|
```
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
```PHP
|
To get started quickly you may copy this into your project.
|
||||||
|
|
||||||
|
```php
|
||||||
use WilliamAAK\Http\Route;
|
use WilliamAAK\Http\Route;
|
||||||
|
|
||||||
Route::get('/', fn() => print('hello world'));
|
Route::get('/$world?', function($world = 'World') {
|
||||||
|
echo "Hello, $world!";
|
||||||
|
});
|
||||||
|
|
||||||
|
// since no route was matched show a 404 page
|
||||||
|
http_response_code(404);
|
||||||
|
?>
|
||||||
|
<h1>Page not found!</h1>
|
||||||
```
|
```
|
||||||
|
|
||||||
See `Route.php` and read the comments for more information
|
The simplest way to access your routes is to put the file in your favorite folder and run it! E.g if you request `http://your.site/yourscript.php/your/route` the route will be automatically converted to `/your/route`
|
||||||
|
|
||||||
## Access directly
|
# Rewrite requests
|
||||||
|
|
||||||
The simplest way to access your routes. Just put the file in your favorite folder and run it!
|
Want to hide that pesky script name (e.g `index.php`) from the URL?
|
||||||
|
|
||||||
For example
|
### FrankenPHP:
|
||||||
|
|
||||||
`http://your.site/folder/index.php/your/route`
|
It is recommended that you use FrankenPHP the modern PHP app server. This behavior is enabled by default.
|
||||||
|
|
||||||
## Rewrite requests
|
### NGINX:
|
||||||
|
|
||||||
The more sexy way of doing it.
|
With PHP already installed and configured you may add this to the server block of your configuration to make requests that don't match a file on your server to be redirected to `index.php`
|
||||||
|
|
||||||
For example
|
|
||||||
|
|
||||||
`http://your.site/your/route`
|
|
||||||
|
|
||||||
### NGINX
|
|
||||||
|
|
||||||
With PHP already configured, add this to the server block of your configuration to make requests that don't match a file on your server to be sent to your front controller and begin routing.
|
|
||||||
|
|
||||||
|
```nginx
|
||||||
location / {
|
location / {
|
||||||
try_files $uri $uri/ /index.php?$query_string;
|
try_files $uri $uri/ /index.php?$query_string;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Apache:
|
||||||
|
|
||||||
|
Make sure that mod_rewrite is installed on Apache. On a unix system you can just do
|
||||||
|
|
||||||
|
`a2enmod rewrite`
|
||||||
|
|
||||||
|
This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to `index.php`
|
||||||
|
|
||||||
|
```apache
|
||||||
|
RewriteEngine on
|
||||||
|
RewriteCond %{REQUEST_FILENAME} !-d
|
||||||
|
RewriteCond %{REQUEST_FILENAME} !-f
|
||||||
|
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
|
||||||
|
```
|
19
Route.php
19
Route.php
@ -20,12 +20,25 @@ use InvalidArgumentException;
|
|||||||
* echo "Welcome to the homepage!";
|
* echo "Welcome to the homepage!";
|
||||||
* });
|
* });
|
||||||
*
|
*
|
||||||
* // Define a middleware
|
* // Define a route with a parameter
|
||||||
|
* Route::get('/user/$id', function ($id) {
|
||||||
|
* echo "User ID: $id";
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* // Define a route with an optional parameter
|
||||||
|
* Route::get('/user/$id?', function ($id = null) {
|
||||||
|
* echo $id ? "User ID: $id" : "No User ID provided.";
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* // Defining middlewares
|
||||||
* Route::before(function () {
|
* Route::before(function () {
|
||||||
* echo "Middleware executed before the route callback.";
|
* echo "Middleware executed before the route callback.";
|
||||||
* });
|
* });
|
||||||
|
* Route::after(function () {
|
||||||
|
* echo "Middleware executed after the route callback.";
|
||||||
|
* });
|
||||||
*
|
*
|
||||||
* // Define a POST route with middleware
|
* // Define a route with a middleware attached to it
|
||||||
* Route::post('/submit', function () {
|
* Route::post('/submit', function () {
|
||||||
* echo "Form submitted!";
|
* echo "Form submitted!";
|
||||||
* }, function () {
|
* }, function () {
|
||||||
@ -34,7 +47,7 @@ use InvalidArgumentException;
|
|||||||
*
|
*
|
||||||
* // Group routes under a common prefix
|
* // Group routes under a common prefix
|
||||||
* Route::group('/admin', function () {
|
* Route::group('/admin', function () {
|
||||||
* // Define a middleware for the group; it will not affect the outer routes
|
* // Middlewares defined here will not affect the routes outside this group
|
||||||
* Route::before(function () {
|
* Route::before(function () {
|
||||||
* echo "Admin Middleware executed.";
|
* echo "Admin Middleware executed.";
|
||||||
* });
|
* });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user