路由

2018-06-26 18:29 更新

可編程路由是 Laravel 的一大特性,而在 Notadd 中,允許以 Event 的形式進(jìn)行擴(kuò)展。

擴(kuò)展示例

第一步:

在 Module 的 ModuleServiceProvider 入口類(lèi)或 Extension 的 Extension 入口類(lèi) 中訂閱 RouteRegister 事件:

  1. $this->app->make(Dispatcher::class)->subscribe(RouteRegister::class);

第二步:

在類(lèi) RouteRegister 實(shí)現(xiàn) router 定義,示例代碼如下:

  1. namespace Notadd\Content\Listeners;
  2. use Notadd\Content\Controllers\Api\Article\ArticleController as ArticleApiController;
  3. use Notadd\Content\Controllers\Api\Article\ArticleTemplateController as ArticleTemplateApiController;
  4. use Notadd\Content\Controllers\Api\Category\CategoryController as CategoryApiController;
  5. use Notadd\Content\Controllers\Api\Category\CategoryTemplateController as CategoryTemplateApiController;
  6. use Notadd\Content\Controllers\Api\Category\CategoryTypeController as CategoryTypeApiController;
  7. use Notadd\Content\Controllers\Api\Page\PageController as PageApiController;
  8. use Notadd\Content\Controllers\Api\Page\PageTemplateController as PageTemplateApiController;
  9. use Notadd\Content\Controllers\Api\Page\PageTypeController as PageTypeApiController;
  10. use Notadd\Content\Controllers\ArticleController;
  11. use Notadd\Content\Controllers\Api\Article\ArticleTypeController as ArticleTypeApiController;
  12. use Notadd\Content\Controllers\CategoryController;
  13. use Notadd\Content\Controllers\PageController;
  14. use Notadd\Foundation\Routing\Abstracts\RouteRegister as AbstractRouteRegister;
  15. /**
  16. * Class RouteRegister.
  17. */
  18. class RouteRegister extends AbstractRouteRegister
  19. {
  20. /**
  21. * Handle Route Registrar.
  22. */
  23. public function handle()
  24. {
  25. $this->router->group(['middleware' => ['auth:api', 'web'], 'prefix' => 'api/article'], function () {
  26. $this->router->resource('template', ArticleTemplateApiController::class);
  27. $this->router->resource('type', ArticleTypeApiController::class);
  28. $this->router->resource('/', ArticleApiController::class);
  29. });
  30. $this->router->group(['middleware' => ['auth:api', 'web'], 'prefix' => 'api/category'], function () {
  31. $this->router->resource('template', CategoryTemplateApiController::class);
  32. $this->router->resource('type', CategoryTypeApiController::class);
  33. $this->router->resource('/', CategoryApiController::class);
  34. });
  35. $this->router->group(['middleware' => ['auth:api', 'web'], 'prefix' => 'api/page'], function () {
  36. $this->router->resource('template', PageTemplateApiController::class);
  37. $this->router->resource('type', PageTypeApiController::class);
  38. $this->router->resource('/', PageApiController::class);
  39. });
  40. $this->router->group(['middleware' => 'web', 'prefix' => 'article'], function () {
  41. $this->router->resource('/', ArticleController::class);
  42. });
  43. $this->router->group(['middleware' => 'web', 'prefix' => 'category'], function () {
  44. $this->router->resource('/', CategoryController::class);
  45. });
  46. $this->router->group(['middleware' => 'web', 'prefix' => 'page'], function () {
  47. $this->router->resource('/', PageController::class);
  48. });
  49. }
  50. }
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)