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?

dongnvwang8591
dongnvwang8591 我会读到laravel。但我正在尝试使用slimphp找到一些解决方案。像一些控制器包,因为我想继续使用slim而不是更改框架。
3 年多之前 回复
dongliang_bj2016
dongliang_bj2016 要更快地构建API,您可能希望切换到Laravel并使用资源控制器。相同的路由语法!
3 年多之前 回复

2个回答

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
    }
}
dsyct08008
dsyct08008 这与我的想法非常相似,并认为我可以将这样的东西应用到我的项目中。 因为我喜欢上课。
3 年多之前 回复

I usually have my own router before I involve Slim's router to determine which route to use based on the path after the domain:

public/index.php

chdir(dirname(__DIR__));

require_once 'vendor/autoload.php';

$app = new Slim\App;
require 'app/routes/index.php';
$app->run();

app/routes/index.php

$_url = parse_url($_SERVER['REQUEST_URI']);
$_routes = explode('/',$_url['path']);
$_baseRoute = $_routes[1];

switch ($_baseRoute) {
    case 'api':
        $_routeFile = 'app/api/' . $_routes[2] . '.php';
        break;

    default:
        $_routeFile = 'app/routes/' . $_baseRoute . '.php';
        break;
}

if (file_exists($_routeFile)) {
    require $_routeFile;
}
else {
    die('Invalid API request');
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐