mirror of
https://github.com/WilliamAAK/Route.git
synced 2025-04-16 09:17:20 +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!";
|
||||
});
|
||||
|
||||
// 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 () {
|
||||
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 () {
|
||||
echo "Form submitted!";
|
||||
}, function () {
|
||||
@ -29,9 +42,9 @@ Route::post('/submit', function () {
|
||||
|
||||
// Group routes under a common prefix
|
||||
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 () {
|
||||
echo "Admin Middleware executed.";
|
||||
echo "Admin Middleware executed.";
|
||||
});
|
||||
Route::get('/dashboard', function () {
|
||||
echo "Admin Dashboard";
|
||||
@ -44,10 +57,10 @@ Route::group('/admin', function () {
|
||||
|
||||
# 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.
|
||||
```JSON
|
||||
```json
|
||||
{
|
||||
"repositories": [
|
||||
{
|
||||
@ -64,44 +77,62 @@ Fetch the package by running
|
||||
|
||||
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'
|
||||
```
|
||||
|
||||
# Usage
|
||||
|
||||
```PHP
|
||||
To get started quickly you may copy this into your project.
|
||||
|
||||
```php
|
||||
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
|
||||
```nginx
|
||||
location / {
|
||||
try_files $uri $uri/ /index.php?$query_string;
|
||||
}
|
||||
```
|
||||
|
||||
`http://your.site/your/route`
|
||||
### Apache:
|
||||
|
||||
### NGINX
|
||||
Make sure that mod_rewrite is installed on Apache. On a unix system you can just do
|
||||
|
||||
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.
|
||||
`a2enmod rewrite`
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.php?$query_string;
|
||||
}
|
||||
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]
|
||||
```
|
21
Route.php
21
Route.php
@ -20,12 +20,25 @@ use InvalidArgumentException;
|
||||
* 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 () {
|
||||
* 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 () {
|
||||
* echo "Form submitted!";
|
||||
* }, function () {
|
||||
@ -34,9 +47,9 @@ use InvalidArgumentException;
|
||||
*
|
||||
* // Group routes under a common prefix
|
||||
* 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 () {
|
||||
* echo "Admin Middleware executed.";
|
||||
* echo "Admin Middleware executed.";
|
||||
* });
|
||||
* Route::get('/dashboard', function () {
|
||||
* echo "Admin Dashboard";
|
||||
|
Loading…
x
Reference in New Issue
Block a user