tsirosgeorge / semantic
一个用于构建Web应用的简单PHP框架。
v1.0.4
2024-09-14 16:09 UTC
Requires
- nikic/fast-route: ^1.3
- phpmailer/phpmailer: ^6.9
- tracy/tracy: ^2.10
- vlucas/phpdotenv: ^5.6
README
欢迎使用Semantic MVC框架!该项目是一个使用FastRoute进行路由的自定义MVC框架,旨在内部使用。它包括像View
类用于渲染HTML视图和基本认证等特性。
安装
要安装Semantic MVC框架,请使用Composer
composer require tsirosgeorge/semantic
如何使用
-
配置
安装后,将文件
.env.example
重命名为.env
,并添加您的配置设置。 -
渲染视图
要在您的视图页面中渲染数据,请使用
View::render
方法。例如View::render('login', $data, 'auth');
- 第一个参数是视图页面名称。
- 第二个参数是要传递给视图的数据。
- 第三个参数是要使用的布局。
在您的视图文件内部,您可以使用占位符,如{{your_value}}
。这些占位符将用您提供的数组中的对应值替换。请确保占位符与您的数据数组中的键匹配。
路由
在定义路由时,请使用以下约定
- API路由:在API路由前缀以
api/
。 - 页面路由:对于常规页面路由,不要使用前缀。
- 分组路由:与前缀一起使用。以下是一个示例。
示例
以下是在public/index.php
中定义路由的示例
要添加受保护的路线,只需在路由定义中将'auth'
作为最后一个参数包含即可。
您还可以使用带有前缀的分组路由
router->group([], function ($router) { $router->addRoute('GET', '/', 'AuthController@showLoginForm'); $router->group(['middleware' => 'auth'], function ($router) { $router->addRoute('GET', '/dashboard', 'DashboardController@index'); $router->addRoute('GET', '/dashboard/members', 'DashboardController@members'); $router->addRoute('GET', '/dashboard/b2binterest', 'DashboardController@b2binterest'); }); }); // Group for API Routes $router->group(['prefix' => '/api'], function ($router) { // Auth Routes $router->addRoute('GET', '/logout', 'AuthController@logout'); $router->addRoute('POST', '/login', 'AuthController@login'); $router->addRoute('POST', '/register', 'AuthController@register'); $router->addRoute('GET', '/refresh-session', 'AuthController@refreshSession'); $router->group(['middleware' => 'auth'], function ($router) { $router->addRoute('GET', '/dashboard/data', 'DashboardController@loadData'); $router->addRoute('POST', '/b2binterest', 'api\B2BInterestController@create'); $router->addRoute('PUT', '/b2binterest/{id}', 'api\B2BInterestController@update'); }); }); In this example: - Routes without the `api/` prefix are used for regular page requests. - Routes with the `api/` prefix are designated for API requests. - To protect a route, add `'auth'` as the third parameter. This indicates that the route requires authentication or add it on a group with name middleware as the example
许可
该项目受MIT许可协议的许可。完整的许可条款可以在LICENSE文件中找到。
变更日志
对该项目的所有重大更改都将记录在CHANGELOG.md文件中。