0

I have 2 controllers. One manages category and other slider. They have no relation between them. They are on same homepage in different sections. So I want to show both category and slider value that is there in database.

I tried

Route::get('/', ['uses'=>'SliderController@homepage','as'=>'homepage']);
Route::get('/', ['uses'=>'CategoryController@homepage','as'=>'categoryHomepage']);

If I do that the second one is overwriting the first one.

Is there a way from which I can pass both routes so that I can use both values in my homepage.

Thanks!

Phoenix
  • 332
  • 2
  • 9
  • 32
  • 2
    maybe you could try to call one method in the other method to get all data at one place and then set the route to that method accordingly – Mehravish Temkar Dec 07 '16 at 05:44
  • @MehravishTemkar can you give a reference site as I don't know how to share values between controllers. – Phoenix Dec 07 '16 at 05:47
  • it's a bit odd, you had exact same `url` and same method, but expect it to behave differently. is there any criteria for it to pass request to the first controller or the second one? if so, you might want to use `middleware` or just multiplex on controller level as Mehravish Temkar suggested. – Bagus Tesa Dec 07 '16 at 05:47
  • @BagusTesa I kept same method name for simplicity. Can you tell me how to implement it or a site where it has implemented it. Thanks. – Phoenix Dec 07 '16 at 05:49
  • Looks like a job for ajax – Derek Pollard Dec 07 '16 at 05:49
  • 1
    http://stackoverflow.com/questions/30365169/access-controller-method-from-another-controller-in-laravel-5 or http://stackoverflow.com/questions/29750913/call-a-method-from-one-controller-inside-another try these and also refer laravel docs – Mehravish Temkar Dec 07 '16 at 05:50
  • @Phoenix, *can you give a reference site as I don't know how to share values between controllers* you could call another controller's function just like calling a class' function. just instantiate target controller properly and call its method. – Bagus Tesa Dec 07 '16 at 05:50
  • just a bit curious, for what reason `Route::get('/',...) ` twice? (same url, same method, two different controller) – Bagus Tesa Dec 07 '16 at 05:54
  • @BagusTesa I have 2 controller One manages category and other slider. They have no relation between them. Just they are on same homepage in different sections. So I want to show both category and slider value that is there in database. That is why I did that – Phoenix Dec 07 '16 at 05:56
  • *on same homepage in different sections* - is it ajax? – Bagus Tesa Dec 07 '16 at 05:58
  • @BagusTesa No. Its completely laravel website with php for backend and bootstrap for frontend. I didnt use any javascript or ajax yet. – Phoenix Dec 07 '16 at 06:01
  • what you can do is, call either of controller's function in another, ex : CategoryController's homepage() can be called from SliderController's homePage() and then pass data from both the function to blade – kapilpatwa93 Dec 07 '16 at 06:02
  • @BagusTesa Html has
    tags so I meant that one section has slider and other has category I needed to get its value from database
    – Phoenix Dec 07 '16 at 06:02
  • @kapil.dev yes I will do that. Thanks – Phoenix Dec 07 '16 at 06:02

1 Answers1

0

Have you tried dependency injection.

1 Single route

Route::get('/home', ['uses'=>'SliderController@index','as'=>'homepage']);

2 Create a data passing controller

use View;

class SliderController extends Controller {

    private $repository;

    public function __construct(DataRepository $repository)
    {
        $this->repository = $repository;
    }

    public function index(DataRepository $repository)
    {
        return View::make('home.index')->with('data', $this-repository->getData());
    }

}

3 DataRepository class fetch there records

class DataRepository {

    public getData()
    {
        $data = array();

        $data['category'] = Category::all();

        $data['slider'] = Slider::all();

        return $data;
    }

}

After above three steps, DataRepository will automatically inject data to your controller. Reference taken from here.

Community
  • 1
  • 1
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57