duanbi8529 2017-07-19 01:36
浏览 95
已采纳

SlimPhp微框架上的Api路由模式?

Is there some pattern of routes and how to write the structure using SlimPhp?

Like, I made a api folder with a index.php to store ALL my routes:

$app->get('/api/shirt/{id}', function (Request $request, Response $response) { 
    //CODE
});
$app->get('/api/some-other-endpoint/{id}', function (Request $request, Response $response)
    //CODE
});

But after some time, I realized that my index file will get pretty big.

So, how can I manage my endpoint routes? Using classes, controllers, actions?

Where can I find documentation about these particular concepts?

  • 写回答

2条回答 默认 最新

  • douba3975 2017-07-19 17:29
    关注

    I'm using controller's (named Action's in this example) and still have all routing in one file.

    Additionally I use grouping whereever I can because it give a better structure (in my opinion). I try to make the Action-classes as small as possible that I do not need to look at the routes-file for getting the class which I want to change.

    Here an example:

    Routes-File:

    $app->get('/user/{name}', [ShowUserAction::class, 'showUser'])->setName('user');
    $app->get('/login', [LoginUserAction::class, 'showLogin'])->setName('login');
    
    $app->group('/api', function () {
        $this->get('/images', [ImagesApi::class, 'getImages'])->setName('api.images');
        $this->get('/tags', [ImagesApi::class, 'getTags'])->setName('api.tags');
        $this->get('/notifications', [UserNotificationsApiAction::class, 'getNotifications'])->setName('api.notifications');
        $this->get('/bubbleCount', [BubbleCountApiAction::class, 'getBubbleCount'])->setName('api.bubbleCount');
    });
    
    $app->group('/review', function() use ($currentUser) {
       $this->get('', [ReviewAction::class, 'showReviewOverview'])->setName('review.overview')->setName('review')
       $this->get('/{type}', [ReviewAction::class, 'showReviewWithType'])->setName('review.type')
       $this->get('/{type}/{id}', [ReviewAction::class, 'showReview'])->setName('review.type.id')
    });
    

    Action-class:

    class LoginUserAction
    {
        public function __construct() { }  // with parameters
    
        public function showLogin(Request $request, Response $response)
        {
            if ($this->currentUser->isLoggedIn()) {
                return $response->withRedirect($this->router->pathFor('index'));
            }
    
            return $this->view->render($response, 'user/login.twig');
        }
    
        public function doLogin(Request $request, Response $response)
        {
            // check user name password and then login
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题