szenis / picro
PHP 微型框架
dev-master
2017-10-09 22:00 UTC
Requires
- pimple/pimple: ^3.0
- symfony/http-foundation: ^3.3
- szenis/routing: ^2.0
This package is not auto-updated.
Last update: 2024-09-29 05:14:38 UTC
README
入门
步骤 1 - .htaccess 文件 在项目根目录创建一个 .htaccess 文件,并填写以下代码
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
步骤 2 - 包含 szenis/picro
在您的终端中执行: composer require szenis/picro
步骤 3 - 创建 index.php
在项目根目录创建 index.php 文件
步骤 4 - 使用框架
以下是一个 index.php 的示例
<?php require './vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $app = new \Szenis\Picro\App(); # It is possible to use the full path to your method $app->get('/', '\App\CoreBundle\Controller\DefaultController::indexAction'); # Or a Closure $app->get('/closure', function() { return new Response('hello world'); }); # The Picro framework uses the request and response object from Symfony # You are required to return a instance of the Response object # The name of the variables in the slug have to match the names of the variable used in the function. # Because the names of the arguments are the same it doesn't matter in which order they are defined $app->get('/{n:number}/{w:word}', function($word, $number) { return new Response('hello world'); }); # When you need the Request or Response object you can simply inject it just by typehinting the class $app->get('/admin/{w:word}', function(Request $request, Response $response, $word) { $response->setContent('hello world'); return $response; }); # After all routes are registerd we can run our application. $app->run();
可选
为了调试目的,请在您的 index.php 中添加以下内容
error_reporting(E_ALL); ini_set('display_errors', 1);
路由 此包使用 "Simple-PHP-Router" (v2),更多信息请查看文档 https://github.com/stein189/Simple-PHP-Router
额外 不要忘记!当您想使用命名空间时,您必须让 composer 知道!
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
关于 psr-4 自动加载的更多信息 http://www.php-fig.org/psr/psr-4/