简体   繁体   中英

Using Laravel/Dingo for an API only

We have a large scale PHP appliction which is basically written in our own framework, from scratch. Now for mobile development we need to create an API, but the question is wether to use just a rest api package, or go for something more robust. Writing out own rest api from scratch is not an option anymore.

I have looked at the option to include the Slim framework . This will be quite easy to implement, but it lacks a good structure in my opinion.

Another option I have looked at is Dingo , which is build on top of Laravel. One great advantage is that it already has a huge structure and large amount of tools to work with.

Question here is, will Laravel be too much overhead to use just for an API, while we have our own framework running on the same server too. Note that we have to include a large part of our own framework in order to keep things running smooth. All models and list will come from our own framework.

I don't know if Laravel will load a lot of unnecessary items before getting to the API part, or if this will be just lightweight as the Slim framework would be.

I don't know if Laravel will load a lot of unnecessary items before getting to the API part, or if this will be just lightweight as the Slim framework would be.

I'm pretty sure that's why Taylor Otwell (the creator of Laravel) made Lumen do check it out and see if it's the right thing for you.

If you decide on Laravel, as of 5.2 you can separate your API end points in your routes file away from the components that are usually loaded in your usual calls to a web page (such as session, cache, etc.) by specifying which middleware should be used.

If you take a look at the routes file, for example:

Route::group(['middleware' => ['web']], function () {
    //
});

Then take a look at the middleware groups in Http\\Kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

You can see that it's already somewhat set up for you not to load up anything unnecessary. So managing your routes and middleware you have great control over what's loaded up in your API and what's not.

All frameworks offer a trade-off between convenience of build over performance.

In general, unless your API is high traffic, it doesn't matter which framework you use and you should use the one you're most comfortable with. The bottlenecks are usually around database use and you should use a profiler to ensure that you tackle the actual performance problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM