Search code examples
phplaraveldirectory-structure

Laravel Structure - Where to place custom API methods?


I'm looking to implement MYOB OAuth API to my application which will run via a cronjob.

(Reference: http://myob-technology.github.io/AccountRight_OAUTH_sample_php/)

According to the laravel structure, which folder would be most appropriate to place this kind of code in?


Solution

  • In Laravel you can place your API codes anywhere you want. But I feel more comfortable keeping my API code in Controllers directory.

    Basic Directory and File Structure I follow

    |- Http
     `|- Controllers
       `|- API
         ` - ApiController.php
           - OtherApiControllers.php
    

    So basically I create a directory API to keep all the API controllers
    The ApiController.php is extended by all other api controllers. The reason is I place some logics which all the other class will inherit.

    For example:

    class ApiController extends Controller {
        public function __construct()
        {
          # Setting Header
          header('Access-Control-Allow-Origin: *');
        }
    }
    

    So yeah, I will recommend to follow this structure as I've been following this structure for almost two years and its going great.