Sharan Mohandas
Sharan Mohandas

Reputation: 31

Code Reuse : Common Functions - Laravel

I'm in the process of Developing APIs for a System whose front end is Android. App calls the API with lat and lon.

I am using Laravel for this and have developed some APIs already. I've also created separate controllers for each and is working correctly.

However 2 (Calculate Distance from Latitude and Longitude and return nearest ones in Ascending order) of the functions are used commonly almost in 3 or 4 Controllers: BusinessController , OfferController, OutletController. I thought it would be better to reuse the code passing parameters correspondingly.

Where should I place the common functions and how should they be invoked? Here is the code for controller.php

<?php namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;

abstract class Controller extends BaseController {

    use DispatchesCommands, ValidatesRequests;

}

PS: I'm somewhat new to Laravel.

Upvotes: 3

Views: 1544

Answers (1)

Iamzozo
Iamzozo

Reputation: 2358

In Laravel 5, the App folder is autoloaded. Create a folder or just a file somewhere in it:

<?php

namespace App; // Or App\Yourfolder

class YourSpecialClass
{
   // Your methods
}

And in your controller, just simply use App\YourSpecialClass;

Upvotes: 4

Related Questions