raoul/mvc-framework

此软件包最新版本(V2.0.2)没有提供许可证信息。

安装: 5

依赖项: 0

建议者: 0

安全: 0

星标: 8

关注者: 1

分支: 0

公开问题: 0

类型:项目

V2.0.2 2023-09-06 16:57 UTC

This package is auto-updated.

Last update: 2024-09-06 19:28:34 UTC


README

安装方法

克隆仓库

git clone https://github.com/RaoulvanWijk/mvc.git

安装composer软件包

composer install

运行框架

要运行框架,您有两种选择:从命令行运行

$ php mvc serve

或通过创建虚拟主机来运行。

注意,mvc服务器在底层使用的是PHP服务器,这**不是**用作Web服务器的目的,因此仅用于开发目的的mvc serve

并确保您正在运行数据库,如果您想使用它的话

路由

转到routes文件夹中的web.php文件

use App\Http\Route;
use App\Http\Controllers\DemoController;

Route::get('url', [DemoController:class, 'method'], "name");
Route::post('url', [DemoController:class, 'method'], "name");
Route::put('url', [DemoController:class, 'method'], "name");
Route::delete('url', [DemoController:class, 'method'], "name");

路由参数

Route::get('url/{id}/another/{param}', [DemoController:class, 'method'], "name");

分组路由

Route::prefix('/demo')->group(function() {
  	Route::get('url', [DemoController:class, 'method'], "route");
    Route::get('url', [DemoController:class, 'method'], "anotherRouteInGroup");
});

中间件

中间件用于验证uri请求

创建中间件

要创建中间件,请执行以下命令

$ php mvc make:middleware NameOfTheMiddleware

将中间件添加到路由

将中间件添加到单个路由

Route::get('url', [DemoController:class, 'method'], "anotherRouteInGroup", middleware: "nameOfMiddleware");

将中间件添加到分组路由

Route::prefix('/demo')->group(function() {
  Route::get('/url', [DemoController:class, 'method'], "route");
}, ["middleware" => ["nameOfMiddleware"]]);

要指定新的中间件,请转到app\Http\HttpKernel,并将您的中间件类添加到

  private $routeMiddleware = [
    "nameOfMiddleware" => \App\Http\Middleware\YoureMiddlewareClass
  ];

控制器

控制器用于在视图和模型之间进行通信

创建控制器

您可以使用以下命令创建控制器

$ php mvc make:controller YoureNameOfTheController

控制器中的路由

class DemoController extends Controller
{
  public function demo()
  {
    // Redirect to different route
    $this->redirect('name_of_route');

    // Redirect with a message
    $this->redirect('name_of_route')->with('key', 'value');

    // Go to previous route
    $this->back();
  }
}

渲染视图

class DemoController extends Controller
{
  public function demo()
  {
    // Filename in resources folder
    $this->view('file');


  }
}

模型

模型用于与数据库交互

创建模型

您可以使用以下命令创建模型

$ php mvc make:model YoureNameOfTheModel

您给模型起的名字也将解析为您的数据库表名,例如

class User

将默认具有users作为表名

您可以通过添加来更改数据库表名

protected $databaseTable = 'tablename';

模型属性

以下是您可以更改的属性列表

   /**
   * Var used to store the database Table of the model
   */
  protected string $databaseTable;

  /**
   * Var used to keep track of tables primary key
   */
  protected string $primaryKey = 'id';

  /**
   * Var used to store all the columns that are allowed to be inserted
   */
  protected array $fillable = [];

模型查询构建器

此模型还具有查询构建器,您可以使用以下示例

  public function Demo()
  {
  // Using not Raw functions are **NOT SAFE** using this without sanitizing the user input is not recommended
    return Progress::select()->where('id = 1')->get();
    
    // using Raw functions are safer since it is using prepared statements with binding
    return Progress::select()->whereRaw('id = :id', [':id', 1])->get();
    
    // But you should still always sanitize the user input
  }

如果您不想使用查询构建器,您也可以使用以下方式执行SQL语句

  public function Demo()
  {
    return Progress::query('SELECT * FROM users');
  }

在每个构建的查询结束时,您需要使用

->get();

来执行SQL语句

请求

在此框架中,您可以使用请求类在数据到达控制器之前对其进行验证,并在出现错误时将用户重定向回上一页

创建请求类

要创建请求类,请使用以下命令

$ php mvc make:request NameOfRequestClass

指定规则

要验证数据,您需要使用以下格式的rules()函数

  public function rules()
  {
    return [
      'username' => 'required|string|min:8',
      'password' => 'required|string|min:8'
    ];
  }

您还可以指定请求失败时要重定向到的URL

$redirect_if_failed = 'name_of_route';

使用authorize()

您可以使用authorize函数来验证用户是否有权发出请求,如果authorize()返回false,它将引发错误

  public function authorize()
  {
    return true;
  }