julien-desca / router
此包的最新版本(0.1.1)没有可用的许可证信息。
0.1.1
2018-08-03 15:41 UTC
Requires (Dev)
- phpunit/phpunit: ^7.2
This package is not auto-updated.
Last update: 2024-10-01 07:38:21 UTC
README
1. 安装
- 使用Composer
composer require julien-desca/router dev-master
2. 使用
- 修改您的 .htacces 如下
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
- 在 index.php 中
<?php require __DIR__ . '/vendor/autoload.php'; $router = new \JDesca\Router\Router($_GET['url']); //register your routes try { //you can pass a string like Controller@action $router->get('your/path', 'Full\Name\To\Controller@actionMethod'); //or you can also pass a callable $router->get('your/path/function', function () { //:foo for parameters echo "foo !!!"; }); //eventually with parameters $router->get('your/path/:id', function ($id) { //:foo for parameters echo ":id paramater is equal to : $id"; }); //you can work with GET POST PUT and DELETE methods $router->post('your/path', 'Full\Name\To\Controller@otherActionMethod'); $router->put('your/path', 'Full\Name\To\Controller@putActionMethod'); $router->delete('your/path', 'Full\Name\To\Controller@deleteActionMethod'); $router->get('', 'TestController@testGet'); $router->get('/:id', 'TestController@testGetWithParam'); /** * then juste run the app */ $router->run(); } catch (\JDesca\Router\Exception\MethodNotAllowedException $e) { } catch (\JDesca\Router\Exception\RouteNotFoundException $e) { } catch (\JDesca\Router\Exception\RouterException $e) { } catch (\Exception $e){ } class TestController{ public function testGetWithParam($id){ echo "you param is : $id"; } public function testGet(){ echo "Get Works!"; } }