inspira/application
Inspra应用设置
dev-master
2024-05-22 09:11 UTC
Requires
- php: ^8.3
- ext-pdo: *
- inspira/framework: dev-master
- ivopetkov/html5-dom-document-php: 2.*
Requires (Dev)
- symfony/var-dumper: 7.1.x-dev
This package is auto-updated.
Last update: 2024-09-22 09:49:03 UTC
README
一个受Laravel启发的简单PHP MVC框架。
免责声明 !!!
此项目仅用于娱乐和教育目的。
如何使用?
通过composer创建项目。
composer create-project inspira/application -s dev <project-name>
注意
- 您可以更改项目名称为您想要的名称
启动应用程序。
php wizard run
注意
- 您可以通过命令中的
port
选项设置自定义端口。 - 您还可以通过命令中的
host
选项设置自定义主机。请确保此主机已添加到您的hosts文件中。 - 要查看所有可用命令,请运行
php wizard
。
特性
⭐路由器
您可以在app
目录中的routes.php
文件中注册您的Web路由。
路由器支持的方法有get
、post
、put
和delete
。
参数如下
Uri
- 字符串
- 必需
处理器
- 数组 | 闭包
- 必需
- 数组处理器应具有[控制器,方法]的形状
中间件
- 数组 | 字符串
- 可选
- 默认为空数组
名称
- 字符串
- 可选
use App\Controllers\HomeController;
/** @var Inspira\Http\Router\Router $router */
$router->get('/user/:id/posts/:post?', [HomeController::class, 'index'], \App\Middlewares\AuthMiddleware::class, 'home');
路由器还支持动态路由参数,包括必需和可选的。
要表示路由参数,只需在前面加上冒号:
,要表示可选路由参数,只需在后面加上问号?
。
您可以通过属性访问或get方法从请求对象中访问这些参数。例如,您注册以下路由/users/:id/posts/:post?
use Inspira\Http\Request;
class HomeController
{
public function index(Request $request)
{
echo $request->id;
// post is optional and might be null so you can use get() and provide a default value
echo $request->get('post', 1);
}
}
⭐中间件
您可以在app/Middlewares
目录内创建自己的中间件。中间件应该扩展Middleware
抽象类,因为它实现了PSR的MiddlewareInterface。
创建您的中间件后,您可以通过修改布尔值$global
属性来指示该中间件是否应在所有请求上运行。如果您希望仅在特定路由上运行中间件,则应将$global
设置为false
并从路由注册中附加中间件。
namespace App\Middlewares;
use Inspira\Http\Middlewares\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class AuthMiddleware extends Middleware
{
// Change the value based on your needs
public bool $global = false;
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Do something with the request object and pass to the next handler or immediately return a response
return $handler->handle($request);
}
}
⭐服务容器
服务容器是这个框架的核心。它处理依赖注入,并通过使用PHP的反射API自动解析这些依赖关系。
服务容器可用于
- 将具体类绑定到抽象类
- 绑定单例类
- 创建具有深层依赖关系的类的实例
目前,容器只能解析有效的类、枚举或接口的注入依赖。但是,您仍然可以将实现绑定到任何单词,并使用make
方法来解决它。
// Example of binding
container()->bind(StorageInterface::class, LocalStorage::class);
// In controller method
public function upload(StorageInterface $storage) {
print_f($storage instanceof LocalStorage::class); // prints out true
}
// Example of singleton binding
// You will get only one instance of Database class throughout the request
container()->singleton(DatabaseInterface::class, Database::class); // or container()->singleton(Database::class) if you don't need to bind it to an interface
// In controller method
public function create(Database $database) {
$database->table(...); // you will get the same instance throughout the request
}
// Example of making an instance
// Let's say that request class has a lot of dependencies, and it's dependencies also have dependencies
// Service Container will do all the work for you to make an instance of it
container()->make(Request::class); // or container()->make('auth') if for example you bound an Auth class to the word `auth`