-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
268 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
# Route Mapping | ||
|
||
## Overview | ||
Route mapping is the recommended modern approach for creating routes in web applications. The `map()` function is utilized to create new routes and append them to their respective actions. | ||
|
||
## Basic Usage | ||
|
||
### Route Map Function | ||
The basic syntax for route mapping is: | ||
```php | ||
Route::map() | ||
``` | ||
|
||
The map function accepts two parameters: | ||
1. Request Method Parameter: Defines the HTTP request method for each instance | ||
2. Route URL Parameter: Defines the actual route URL path | ||
|
||
### Supported HTTP Methods | ||
The following HTTP request methods are supported: | ||
- GET | ||
- POST | ||
- PUT | ||
- PATCH | ||
- DELETE | ||
|
||
Multiple methods can be defined for each route instance by separating them with '|': | ||
```php | ||
"GET|POST" // This will match both GET and POST requests | ||
``` | ||
|
||
## Pattern Matching Routes | ||
|
||
### Using Custom Patterns | ||
Route mapping supports custom pattern matching for request URIs. Patterns are declared using the following syntax: | ||
```php | ||
Route::map(GET, 'pattern: {{your_pattern}}') | ||
``` | ||
|
||
### Pattern Syntax | ||
- Must start with `pattern:` keyword | ||
- Uses fnmatch() style pattern matching | ||
- Common patterns: | ||
- `pattern: *` - Matches all request URIs | ||
- `pattern: /admin/*` - Matches any URI that starts with /admin/ | ||
|
||
Example: | ||
```php | ||
Route::map(GET, 'pattern: /users/*')->action(function() { | ||
// Handles any URI starting with /users/ | ||
}); | ||
``` | ||
|
||
## Nested Routes | ||
|
||
### Mapping Routes to Base Route | ||
You can map additional routes to a base route, creating a hierarchical structure: | ||
|
||
```php | ||
Route::map(GET, "/admin")->action() | ||
->Route::route("/dashboard", function(Request $req, Closure $accept) { | ||
$accept(GET, fn($method) => "`$method` method is not allowed"); | ||
return "Admin Dashboard"; | ||
}) | ||
``` | ||
|
||
### Nested Route Parameters | ||
1. First parameter: Request URI path | ||
2. Second parameter: Closure or callable function | ||
- The closure receives two arguments: | ||
- `$req`: Request header information | ||
- `$accept`: Function to handle method acceptance | ||
|
||
### Accept Function | ||
- Used to specify allowed HTTP methods | ||
- Takes two parameters: | ||
- Allowed method(s) | ||
- Fallback function for unallowed methods | ||
- The fallback receives the actual request method as an argument | ||
|
||
Example accessing nested route: | ||
``` | ||
http://localhost:2200/admin/dashboard | ||
``` | ||
|
||
## Case Sensitivity | ||
|
||
### Route Case Sensitivity | ||
By default, all routes are case insensitive. To make a route case sensitive, use the `caseSensitive()` method: | ||
|
||
```php | ||
Route::map(GET, "/Admin")->caseSensitive()->action(function() { | ||
// This route will only match exact case "/Admin" | ||
}); | ||
``` | ||
|
||
### Default Behavior | ||
- Case insensitive: `/admin` will match `/Admin`, `/ADMIN`, etc. | ||
- With `caseSensitive()`: Only exact case matches will work | ||
|
||
## Map Action Methods | ||
|
||
### 1. file() Method | ||
- Takes 1 parameter which specifies the view file path to be rendered | ||
- Used for direct view file rendering | ||
- Example: | ||
```php | ||
Route::map("GET", "/index")->file("::index"); | ||
``` | ||
|
||
### 2. use() Method | ||
- Used in adding controller class methods | ||
- Requires controller class name followed by double colon and method name | ||
- Example: | ||
```php | ||
Route::map(GET, "/index")->use("UserController::index"); | ||
``` | ||
Note: When following the MVC pattern, you don't need to specify the controller namespace. | ||
|
||
### 3. action() Method | ||
- Called directly with the response to be rendered | ||
- Supports multiple formats: | ||
|
||
#### a. Using Controller Methods | ||
```php | ||
Route::map()->action([Controller::class, "method"]); | ||
``` | ||
|
||
#### b. Using Closure Functions | ||
```php | ||
Route::map()->action(function(){ return "Hello"; }); | ||
``` | ||
|
||
#### c. Using Request Parameters | ||
```php | ||
Route::map()->action(function(Request $req) { ... }); | ||
``` | ||
|
||
#### d. Direct String Output | ||
```php | ||
Route::map()->action("Hello World"); | ||
``` | ||
|
||
## Special Cases | ||
|
||
### Functional Arrays | ||
You can call functional arrays using: | ||
```php | ||
Route::map()->action([Controller::class, "method"]); | ||
``` | ||
This will execute the method function in that class and display it on the web. | ||
|
||
### Default String Rendering | ||
When a default string is passed as an argument in the parameter: | ||
```php | ||
Route::map()->action(view::render("::index")); | ||
``` | ||
The view file will be the output when they navigate to the route. | ||
|
||
### Simple String Output | ||
For basic string output: | ||
```php | ||
Route::map()->action("Hello World"); | ||
``` | ||
This will display "Hello World" on the web page. | ||
|
||
## Notes | ||
- For each function called, the Request will be passed as the argument in the parameters if set | ||
- By default, it will output any result it is given | ||
- Views are typically rendered using the view function: `View::render("Folder::File")` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
## **Route Handling and Request Using Controller Closure** | ||
|
||
### **Introduction** | ||
In **PhpSlides** framework, routing helps in connecting HTTP requests to specific controller methods, enabling dynamic and organized request handling. This documentation demonstrates creating routes, utilizing controllers, and handling requests. | ||
|
||
### **Creating a Controller** | ||
Controllers are the backbone of handling logic for specific requests. To create a controller, follow these steps: | ||
|
||
#### **1. Automatic Creation:** | ||
Run the following command to generate a controller automatically: | ||
```bash | ||
phpslides make:controller UserController | ||
``` | ||
|
||
#### **2. Manual Creation:** | ||
Create a file manually in the `app/Http/Controller/` directory. Make sure the file and class names end with `Controller`. For example: | ||
- File: `UserController.php` | ||
- Class: `UserController` | ||
|
||
--- | ||
|
||
### **Example of a Controller** | ||
A typical controller looks like this: | ||
```php | ||
<?php | ||
namespace App\Http\Controller; | ||
|
||
use PhpSlides\Core\Http\Request; | ||
|
||
final class UserController { | ||
public function index(Request $req) { | ||
return "Hello User"; | ||
} | ||
} | ||
?> | ||
``` | ||
|
||
--- | ||
|
||
### **Routing to a Controller Method** | ||
Routes link specific HTTP methods and URLs to controller methods. You can define routes as follows: | ||
|
||
#### **1. Basic Route Definition:** | ||
Use the namespace directly: | ||
```php | ||
Route::get("add-item", [\App\Http\Controller\UserController::class, "index"]); | ||
``` | ||
|
||
#### **2. With Namespace Declaration:** | ||
Alternatively, add the namespace at the top of the file: | ||
```php | ||
<?php | ||
use App\Http\Controller\UserController; | ||
|
||
Route::get("add-item", [UserController::class, "index"]); | ||
?> | ||
``` | ||
|
||
--- | ||
|
||
### **Controller Method Convention** | ||
By convention, specific methods in a controller correspond to standard HTTP requests: | ||
|
||
1. **index()** | ||
- Used for handling `GET` requests without URL parameters. | ||
- Example: Fetching a list of items. | ||
|
||
2. **show()** | ||
- Handles `GET` requests with one or more URL parameters. | ||
- Example: Viewing a specific item by ID. | ||
|
||
3. **destroy()** | ||
- Handles `DELETE` requests with one or more URL parameters. | ||
- Example: Deleting a specific resource. | ||
|
||
4. **store()** | ||
- Handles `POST` requests. | ||
- Example: Creating a new resource. | ||
|
||
5. **update()** | ||
- Handles `PUT` requests. | ||
- Example: Updating an existing resource. | ||
|
||
6. **patch()** | ||
- Handles `PATCH` requests. | ||
- Example: Partially updating a resource. | ||
|
||
--- | ||
|
||
### **Conclusion** | ||
By combining routing and controllers, handling requests becomes structured and scalable. The predefined methods (index, show, etc.) ensure uniformity across routes, simplifying development. Use these principles to enhance your application's functionality and maintainability. | ||
|
||
--- |