lassehaslev / laravel-package-router
该包最新版本(0.1.2)没有可用的许可信息。
0.1.2
2016-12-12 16:56 UTC
Requires
Requires (Dev)
- orchestra/testbench: 3.3.x
- phpunit/phpunit: ^5.6
This package is not auto-updated.
Last update: 2024-09-17 09:45:48 UTC
README
让你的包路由变得非常简单!
安装
运行 composer require lassehaslev/laravel-package-router
用法
这是一个 全局/通用 对象,用于处理你的路由。
尽管这个包是为了处理包中的路由而制作的,但使用它在任何地方都不会有问题。
注册路由
在 ServiceProvider@register 中添加你的路由
// It is recommended that you extend the class class MyRouter extends LasseHaslev\LaravelPackageRouter\PackageRouter {} // Create new router $router = MyRouter::create(); // Add route to router $router->add( 'users.index', [ 'uri'=>'users', 'as'=>'users.index', 'uses'=>'Controller@index', ] ) // You can also chain add ->app( 'users.update', [ 'uri'=>'users/{user}', 'method'=>'put', 'uses'=>'Controller@index', ] ); ->add( 'images.index', [ 'uri'=>'images', 'uses'=>'Controller@index', ] ) ->add( 'images.store', [ 'uri'=>'images', 'method'=>'post', 'uses'=>'Controller@store', ] )
注册路由
注意,即使你在其他地方创建了它,也可以通过调用 MyRouter::get()
来获取路由器的引用。这是因为它扩展了 LasseHaslev\UniversalObjects\Object。
// Usually in your routes/web.php $myRouter = MyRouter::get(); $myRouter->route( 'images.index' ); // "/images" Route::group([ 'prefix'=>'backend', 'middleware'=>'auth' ], function( $router ) use ( $myRouter ) { $myRouter->routes( 'users' ); // "/backend/users" and "/backend/users/{user}" $myRouter->route( 'images.store' ); // "/backend/images" });
API
// Get the router $router = MyRouter::create(); // MyRouter::get(); // Add route $router->add( $reference, [ 'uri'=>'users', 'method'=>'get', 'as'=>'users.index', 'uses'=>'Controller@index', // 'middleware'=>'auth', // You can add middleware if you want to ] ); // Get all routes $router->routes(); // Set routes for users.index, images.index and images.show // Get all routes under images namespace $router->routes( 'images' ); // Set routes for images.index and images.show // Get single route $router->route( 'images.index' );
开发
# Install dependencies composer install # Install dependencies for automatic tests yarn # Run one time npm run test # Automaticly run test on changes npm run dev