简体   繁体   中英

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. fe 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']));

}

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'));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM