0

How can call a controller's method inside another controller method?
I donno this question does make any sense but assume I have a sidebar, which contains a bunch of database data, user info and etc... .
In normally in any controller which needs the sidebar layout, I should call all models and get needed data to pass them to view as sidebar information.
in php OOP, it's simple to call another class method to do this. for example

$sidebar = $class->make_sidebar();

How can I do something like this in Laravel?
This $class->make_sidebar() method should be a model?

Sorry I am new to MVC and have a bit problem about MVC Concepts may be.
Thank you.

1 Answer 1

1

use View::composer()

View::composer('sidebar', function($view)
{
    $view->with('count', User::count());
});

then whenever you call the sidebar, the data will be automatically populated.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please provide me an example. I tried to follow laravel documentation but its not working. inside my get_index controller I want to make a profile view which uses a default layout. inside the default layout I have a sidebar that I want to bound some information to it automatically. I tried this code: View::composer('sidebar','Sidebar'); $v = View::make('profile'); return $v->render(); and inside library class Sidebar { public function compose( $view ){ $view->with('sidebar', 'this is sidebar'); } } thanks.
You cannot call the controller method in another controller. Please follow the repository design method to make functions independent of controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.