Last active
March 28, 2016 18:55
-
-
Save daniellima/9b9a608cfb227a676822 to your computer and use it in GitHub Desktop.
A controller that allows you to pass data to views using the controller instance variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| A controller that allows you to pass data to views using the controller instance variables |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers; | |
| use Illuminate\Foundation\Bus\DispatchesJobs; | |
| use Illuminate\Routing\Controller as BaseController; | |
| use Illuminate\Foundation\Validation\ValidatesRequests; | |
| use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | |
| class Controller extends BaseController | |
| { | |
| use AuthorizesRequests, DispatchesJobs, ValidatesRequests; | |
| public $data; | |
| public function __set($name, $value){ | |
| $this->data[$name] = $value; | |
| } | |
| public function callAction($method, $parameters) | |
| { | |
| $response = parent::callAction($method, $parameters); | |
| if(get_class($response) === \Illuminate\View\View::class){ | |
| $response->with($this->data); | |
| } | |
| return $response; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class SomeController { | |
| public function index(){ | |
| $users = User::all(); | |
| return view('index')->with('users', $users); | |
| } | |
| // When you use this controller as your Base, you can do the same with | |
| public function index(){ | |
| $this->users = User::all(); | |
| return view('index'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment