Tuomas Tuokkola
Tuomas Tuokkola

Reputation: 39

Accessing Controllers methods for sidebar in Laravel

I am making a project with Laravel 5.6 and at the moment I am making a sidebar with links to access the functionality of specified controller. f.e. if I am in posts blade, it will show PostsController methods for the sidebar.

The problem is that every controller has different amount of methods, and I wouldn't want to make a mess with 10 different static layouts for sidebars.

Is there a way to access controller methods thru functionality that returns all methods of the controller to a view?

Or am I thinking this wrong.. If someone knows a better solution for this i'm all ears. :)

I know I can install packages for functionality but I want to know before that is there any simple solution.

EDIT1:

get_class_methods($this) returns following value:

Returned Methods of a Controller

I can add a validator that checks if "index" or "create" is present. Guess my problem was solved, thank you all who answered.

EDIT2:

The code that dumps the returned methods.

public function index()
{

    $events = Event::all();

    dd($controller = get_class_methods($this));

    return view('events.index', compact(['events', 'controller']));

}

Upvotes: 0

Views: 780

Answers (1)

Oldenborg
Oldenborg

Reputation: 936

You could use the ´get_cass_methods´ function to grab all the methods on the controller class

function index() {
  $methods = get_class_methods($this);
  return view('posts', compact('methods'));
}

if you want to filter out methods from the parent class

function index() {
  $methods = array_diff(get_class_methods($this),get_class_methods(get_parent_class()));
  return view('posts', compact('methods'));
}

Upvotes: 1

Related Questions