OunknownO OunknownO - 4 months ago 21
PHP Question

How to use methods from one controller in another

I have installed waavi package for manipulation of translation files. I need to use methods from it's controller to mine? I tried something like this but it doesn't work

LanguageRepository::findByLocale(1);


This is what I am using in beginning of my controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Waavi\Translation\Repositories\LanguageRepository;

use Waavi\Translation\Repositories\TranslationRepository;

use Illuminate\Foundation\Application;

Answer

If you have successfully done all the steps in here, you should be able to access to LanguageRepository using depedency injection(" It is recommended that you instantiate this class through Dependency Injection")

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;

class DefaultController extends Controller
{
    private $language_repository;

    function __construct(LanguageRepository $language_repository)
    {
        $this->language_repository = $language_repository;
    }

    public function index()
    {
        dd($this->language_repository->findByLocale("en"));
    }
}

Note: you need pass language string instead of id to findByLocale method. see line 97

Comments