0

I'm planning to build a web application in Laravel. So far so good, but it needs a JSON RESTful API as well.

What's the best way to go about this? Should I build a separate Laravel API and Laravel client, or is it better to have one application with both JSON and HTML representations?

There are probably benefits to gain from separating the two, although I can't really see them at this point. The downsides are obvious however, having to maintain two code bases and having to implement REST consuming functionality in the client.

Any other options out there? Pros and cons?

2 Answers 2

1

Namespace everything. You can keep it all inside the same application then. There is no point in maintaining 2 code bases because you will have to repeat your business logic in 2 places.

In your routes, then you can then do this

Route::controller('user', 'UserController');

Route::group(['prefix' => 'api', 'namespace' => 'Api'], function() {
    Route::controller('user', 'Api\UserController');
});

Also, don't write your business logic in your controllers. Use commands (known as jobs in Laravel 5.1) and repositories instead.

Pretend you have a Create User feature. Then you will have a corresponding Command/Job class for that.

namespace App\Jobs;

use App\Repositories\UserRepository;
use App\Jobs\Job;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class CreateUser extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $user;

    public function __construct(UserRepository $user)
    {
        $this->user = $user;
    }

    public function handle(Mailer $mailer)
    {
        // logic to create user
    }
}

Which you will use in your UserController

public function postCreateUser()
{
    // validate request

    $this->dispatch(new CreateUser($inputData));

    // return view
}

and then your Api\UserController

public function postCreateUser()
{
    // validate request

    $this->dispatch(new CreateUser($inputData));

    // return JSON output
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would chose separation. I like to approach like this:

//routes.php
Route::group([], function()
{
    Route::match(['get', 'post'],'/', ['as' => 'homepage', 'uses' => 'SiteController@index']);
    //other frontend routes
});

Route::group(['namespace' => 'Admin'], function()
{
    Route::match(['get', 'post'],'/admin', ['as' => 'admin', 'uses' => 'SiteController@index']);
    //other admin/backend routes
});

Route::group(['namespace' => 'Rest'], function()
{
    Route::match(['get', 'post'],'/rest', ['as' => 'rest', 'uses' => 'RestController@index']);
    //other rest routes
});

Controllers for frontend in Controllers folder. Controllers for admin in Controllers/Admin folder. Controllers for rest in Controllers/Rest folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.