Skip to content

Commit

Permalink
add : closure can be used in route
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownRori committed Feb 9, 2022
1 parent 7f34917 commit 21cf487
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Lastly, after the client get the response the `Runtime Middleware` will fired fo

# Usage

`WIP` For usage please read [this](https://github.com/UnknownRori/UnknownRori-PHP/blob/master/core/docs/usage.md) for more information.
For usage please read [this](https://github.com/UnknownRori/UnknownRori-PHP/blob/master/core/docs/usage.md) for more information.

# Roadmap

Expand Down
16 changes: 0 additions & 16 deletions app/model/Posts.php

This file was deleted.

3 changes: 0 additions & 3 deletions app/model/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

namespace App\Model;

use App\Models\Posts;
use Core\Model;


class Users extends Model
{
protected $table = "users";
protected $hasMany = [Posts::class, 'users_id'];
}
5 changes: 3 additions & 2 deletions app/route/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

use Core\Support\Http\Route;

Route::get('/', [Welcome::class, 'index'])->name('home');
Route::post('/post', [Welcome::class, 'post'])->name('post');
Route::get('/', function () {
return view("welcome");
})->name('home');
42 changes: 27 additions & 15 deletions core/Support/http/route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,20 @@ public static function group(callable $callback)
* This method is used to automatic register the route
* @param string $method Route HTTP Request Type
* @param string $uri Route URI
* @param array $controller Route Controller
* @param array|callable $controller Route Controller
*/
protected static function setRoute(string $method, string $uri, array $controller)
protected static function setRoute(string $method, string $uri, array|callable $controller)
{
self::$route[$method][$uri] = [
"controller" => $controller[0],
"action" => $controller[1],
];
if (is_callable($controller)) {
self::$route[$method][$uri] = [
"action" => $controller
];
} else {
self::$route[$method][$uri] = [
"controller" => $controller[0],
"action" => $controller[1],
];
}
}

/**
Expand Down Expand Up @@ -224,20 +230,25 @@ public function Run($uri, $requestType)
*/
protected function call($route)
{
$namespacedController = "App\Http\Controller\\{$route['controller']}";

$controller = new $namespacedController;
$action = $route['action'];

if (array_key_exists('middleware', $route)) {
Middleware::Run($route['middleware']);
}

if (!method_exists($controller, $action)) {
KernelException::ClassMethod($route['controller'], $action);
}
if (is_callable($route['action'])) {
return call_user_func($route['action']);
} else {

return $controller->$action();
$namespacedController = "App\Http\Controller\\{$route['controller']}";
$controller = new $namespacedController;
$action = $route['action'];


if (!method_exists($controller, $action)) {
KernelException::ClassMethod($route['controller'], $action);
}

return $controller->$action();
}
}

/**
Expand All @@ -257,6 +268,7 @@ protected static function temp($key, $value = null)

/**
* This is to dump out all the current static property
* @return array
*/
public static function dump()
{
Expand Down
75 changes: 70 additions & 5 deletions core/docs/usage.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Usage Documentation(WIP)
# Usage Documentation

## Table of Content

- The Basic
- The Basi

- Routing

Expand Down Expand Up @@ -112,13 +112,29 @@

- Table Name

- Model Relationship

- Defining Relationship

- Has Many

- Belongs To

- List Method

# The Basic

## Routing

The most basic UnknownRori-PHP route is accept URI and Controller Class and it's method, very simple and OOP behavior without complicated configuration.
The most basic Rori-PHP routes accept a URI and a closure, provide a simple and expressive method of defining routes and behavior without complicated routing configuration files:

Route::get('/', function () {
return view("welcome");
});

Instead of defining all of your request handling logic as closures in your route files, you may wish to organize this behavior using `controller` classes. Controllers can group related request handling logic into a single class. For example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting users. By default, controllers are stored in the `app/http/controller` directory.

You can define a route to this controller method like so:

Route::get('/', [Home::class, 'index']);

Expand Down Expand Up @@ -176,8 +192,6 @@ Middleware provide a convenient mechanism for inspecting and filtering HTTP requ

### Defining Middleware

#### WIP

To create new middleware, use `make:middleware` cli command

php cli make:middleware EnsureAdmin
Expand Down Expand Up @@ -695,6 +709,57 @@ Database model will always assume that database table has primary key `id`. If n

protected $primary_key = 'posts_id';

### Model Relationship

Database table are often related to one another, for example blog post may have many comments or and order could be related to to the user who place it

#### Defining Relationship

Defining the relationship inside your model is very simple, by using protected object called hasMany or belongsTo

##### Has Many

A one-to-many relationship is used to define relationships where a single model is the parent to one or more child models. For example, a user may have an infinite number of posts.

<?php

namespace App\Model;

use App\Models\Posts;
use Core\Model;


class Users extends Model
{
protected $table = "users";
protected $hasMany = [Posts::class, 'users_id'];
}

To use the relationship that we already define:

Users::find(1, 'hasMany');

##### Belongs To

Now that we can access all of a user's posts, let's define a relationship to allow a posts to access its parent user. To define the inverse of a hasMany relationship, define a relationship method on the child model which calls the belongsTo method:

<?php

namespace App\Models;

use App\Model\Users;
use Core\Model;

class Posts extends Model
{
protected $table = 'post';
protected $belongsTo = [Users::class, 'users_id'];
}

To use the relationship that we already define:

Posts::find(1, 'belongsTo');

### List Method

- all
Expand Down

0 comments on commit 21cf487

Please sign in to comment.