In laravel 4 i just used a function
$varbl = App::make("ControllerName")->FunctionName($params);
to call a controller function from a my balde template(view page). Now i'm using Laravel 5 to do a new project and i tried this method to call a controller function from my blade template .But its not working and showing some errors. Is there any method to call a controller function from a view page in Laravel 5?
Actually below should work as per
Laravel 5
:Usage:
App::make(ControllerName)->functionName($parameters);
Example:
App:make("TestController")->getUserInfo('user_id' => 9);
Please post the actual error you are getting!
Just try this in your view :
OR
Refer this : http://laravel.io/forum/03-06-2014-what-is-the-proper-way-to-call-controllers-from-the-view?page=1
If you have a function which is being used at multiple places you should define it in helpers file, to do so create one (may be) in app/Http/Helpers folder and name it helpers.php, mention this file in autorun block of your composer.json in following way :
run composer dump-autoload, and then you may call this function from anywhere, let it be controller view or model.
or if you don't need to put in the helpers. You can just simply call it from it's controller. Just make it a
static function
. Create.Call.
If you don't like the very long code, you can just make it
but don't forget to call it from the top of the view page.
For accessing a method of a Controller from a view page you need to give the full path of that controller in your blade page.
Here AdminAfterAuth is the controller class name and globAdmin is the method name.
Now in your controller declare the method statically.
I like and favor Khan Shahrukh way, it is better to create a helpers files with all your functions, then add it to your composer.json file:
You can select the path that suits you, then dump-autoload composer to make it includes the new file.
For usability and clean work, after that you will be able to invoke your function on any view file OR project parts : Controller, Model.
If you decided to go with the public static method Don't forget to add this line at the very top of your view:
In laravel 5, you can do it like so
In your view:
The functionName should have the 'static' keyword e.g
Controller function: