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
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…