Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
dconco committed Jan 19, 2025
1 parent 3d1f6e2 commit 074c5e4
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@

### 2.1 Routing System
- [Basic Route Handling](/web_routing.md)
- HTTP Methods and Closures
- [HTTP Methods and Closures](/web_routing.md)
- [GET Requests](/web_routing.md#get-route-with-closure)
- [POST Requests](/web_routing.md#post-route-with-closure)
- [PUT Requests](/web_routing.md#put-route-with-closure)
- [PATCH Requests](/web_routing.md#patch-route-with-closure)
- [DELETE Requests](/web_routing.md#delete-route-with-closure)
- [Route Parameters](/route_parameters.md)
- Named Routes
- Route Groups
- Protected Routes
- Route Mapping
- [Route Mapping](/route_mapping.md)
- [Pattern Matching Routes](/route_mapping.md#pattern-matching-routes)
- [Case Sensitivity](/route_mapping.md#case-sensitivity)
- [Nested Routes](/route_mapping.md#nested-routes)
- [Special Cases](/route_mapping.md#special-cases)

### 2.2 View System

Expand Down
169 changes: 169 additions & 0 deletions route_mapping.md
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")`
93 changes: 93 additions & 0 deletions route_with_controller_closure.md
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.

---

0 comments on commit 074c5e4

Please sign in to comment.