Often times, you want to offer your users other ways to interact with your package. A common usecase is to give a REST API in your package to handle data.

For Laravel-FAQ I originally wanted a nice Single Page App interface for the front-end. In preparation for this, I worked to create a flexible REST API for the future when I may want to add this functionality.

To start, create a route group prefixed with 'api' within one of your customizable route groups. While this could be customizable with a configuration option, I'm choosing to opt into some convention in order to tame somethings in. Similarly, to reduce headaches later we will use raw Route::get, Route::post, and Route::delete to handle our various actions and give us the ability to reference them using route('laravel-faq::api.questions.*').

So our route group looks a bit like this:

Route::group(array('prefix' => 'questions'), function()
{
    Route::get('/', array('uses' => 'Rtablada\LaravelFaq\Api\QuestionsController@index'));
    Route::get('all', array('uses' => 'Rtablada\LaravelFaq\Api\QuestionsController@all'));

    Route::post('/', array('uses' => 'Rtablada\LaravelFaq\Api\QuestionsController@store'));

    Route::get('search', array('uses' => 'Rtablada\LaravelFaq\Api\QuestionsController@search'));

    Route::get('{id}', array('uses' => 'Rtablada\LaravelFaq\Api\QuestionsController@show'));
    Route::delete('{id}', array('uses' => 'Rtablada\LaravelFaq\Api\QuestionsController@delete'));
});

Now we just need to look at our controller:

<?php namespace Rtablada\LaravelFaq;

use Rtablada\LaravelFaq\Repositories\FaqRepository;
use View, Session, Redirect, Input, Config;

class QuestionsController extends BaseController
{
    protected $faqRepo;

    public function __construct(FaqRepository $faqRepo)
    {
        $this->setupViews();
        $this->faqRepo = $faqRepo;
    }

    public function index()
    {
        $faqs = $this->faqRepo->paginate();

        return View::make('laravel-faq::home', compact('faqs'));
    }

    public function create()
    {
        $input = Session::getOldInput();

        return View::make('laravel-faq::questions.create', compact('input'));
    }

    public function store()
    {
        $input = Input::all();

        if ($this->faqRepo->create($input)) {
            Session::flash('success', 'Your Question Has Been Updated');
            return Redirect::route('laravel-faq::index');
        } else {
            return Redirect::back()->withInput();
        }
    }

    protected function setupViews()
    {
        $paths = Config::get('laravel-faq::views.paths');

        foreach ($paths as $path) {
            View::addLocation($path);
        }
    }
}

Quick and easy right? The thing to remeber is that Package Developmnet really isn't much different than developing a regular Laravel Application! It just requires a few extra steps and decisions to add that extra layer of flexibility to make it easy to plug and play in your next project.