danidoble / routing
以symfony-routing为起点的一种简单方法
v1.0.3
2023-02-17 19:10 UTC
Requires
- php: ^8.1
- ext-json: *
- illuminate/support: ^v10.0.3
- ramsey/uuid: ^4.7.3
- symfony/routing: ^v6.2.5
Requires (Dev)
- spatie/ignition: ^1.4.3
- vlucas/phpdotenv: ^v5.5.0
Suggests
- spatie/ignition: Render errors in a beautilful page
- symfony/error-handler: Render errors in page
README
以symfony/routing为起点的一种简单方法
版本^v1.0需要PHP 8.1
composer require danidoble/routing
Debug::enable();
这仅用于调试模式,将其更改为自定义页面,请检查symfony/error-handler
创建一个名为 .env
的文件,内容如下
APP_URL="https:///"
APP_DEBUG=true
APP_VIEW_DIR="views"
将localhost更改为您的网站URL
use Symfony\Component\ErrorHandler\Debug; use Danidoble\Routing\Route; use Danidoble\Routing\Testing; include __DIR__ . "/vendor/autoload.php"; // This is only for debug mode, change it for a custom page, check [symfony/error-handler](https://github.com/symfony/error-handler) Debug::enable(); $base_path = __DIR__ . DIRECTORY_SEPARATOR; // base path of your project $base_view_path = env('APP_VIEW_DIR','views'); // route of your directory of views \Danidoble\Routing\View::$BASE_VIEW_PATH = $base_path . env('APP_VIEW_DIR'); // example: /home/your/project/views $dotenv = Dotenv\Dotenv::createImmutable(__DIR__"); $dotenv->load();
实例化 \Danidoble\Routing\Route;
的Route
$routes = new Route(); //add routes $routes->add('/', [Testing::class, 'index'])->setMethods(['GET','POST']); //only get and post allowed $routes->add('/help', [Testing::class, 'index'])->setMethods('GET'); //only get allowed $routes->add('/danidoble', [Testing::class, 'index', 'a']); // all methods allowed // dispatch $routes->dispatch();
创建路由示例
$path = "/lorem";//route slug $controller = \Danidoble\Routing\Testing::class; // class/controller $method = "index"; // method to execute $route_name = "example_route"; // name of route $routes->add($path,[$controller,$method,$route_name]);
在您的控制器中
class Testing extends \Danidoble\Routing\Controller { public function index() { //dump($this->getParent()); //dump($this->param('demo')); dump($this->getParams()); } }
如果您想重写 __construct()
,请确保应用一些如下代码
public function __construct(RequestContext $_context, RouteCollection $_routes, UrlMatcher $_matcher, Route $_parent) { parent::__construct($_context, $_routes, $_matcher, $_parent); // ... your code here }