2

I'm trying to call dynamic popup views in which I need to pass the data through the controller, I want the controller to be dynamic which will access the particular function and make the view accordingly. Basically i'm looking for something like this:

Route::post('/popup/{id}', 'PopupController@{$id}');

So basically suppose when it is called like this: mydomain.com/popup/id1, it should call PopupController@id1.

Help me out with this.

Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148

2 Answers2

2

I suggest instead of writing a dynamic route or controller use switch case in controller action.

e.g.

Route::post('/popup/{id}', 'PopupController@action');

In Controller

public function action($id)
{
  switch($id)
  {
     case 1: ...

     case 2: ...
  }
}
Akshay Deshmukh
  • 1,242
  • 1
  • 16
  • 31
  • I can use this, but I'm collecting data from dom element and making the view, each switch case will be too lengthy and messy for me. – Nitish Kumar Aug 11 '16 at 19:05
  • I prefer that using dynamic call instead of writing each possibilities exhaustively in order to prevent adding cases manually in the future. – Benyi Apr 01 '18 at 08:32
2

You need a method that will fire the appropriate function

Route::post('/popup/{id}', 'PopupController@dispatch');

In PopupController

public function dispatch($id)
{
    return $this->$id()
}

then if your $id is someFunction you need to make sure your controller has function someFunction() method

Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
Pawel Bieszczad
  • 12,925
  • 3
  • 37
  • 40