Использовать черты.
<?php
namespace App\Http\Controllers\Traits;
use App\Services\ArticleService;
trait CountriesDataTrait
{
public function addCountriesData(&$data = [])
{
$data['countries'] = array(
["code" => "fr","title" => "French", "flag" => "https://www.makkumbeach.nl/img/flag_fe.gif"],
["code" => "es","title" => "Spain", "flag" => "https://www.eurojobs.com/templates/Eurojobs/main/images/flags/Spain.gif"]
);
return $data;
}
}
Использовать черту в контроллерах
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use App\Http\Controllers\CountriesDataTrait;
class FirstController extends Controller
{
use CountriesDataTrait;
public function method()
{
$data = [
// some data
];
$data = $this->addCountriesData($data);
// your logic
}
}
Использовать ту же черту secondController
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use App\Http\Controllers\CountriesDataTrait;
class SecondController extends Controller
{
use CountriesDataTrait;
public function method()
{
$data = $this->addCountriesData();
// your logic
}
}