In laravel 4 i just used a function
$varbl = App::make(\"ControllerName\")->FunctionName($params);
to call a controller function from a
You can actually call a class, helper class or any declared class in your blade template but putting it in the aliases array of your app.php in the config folder
'Helper' => App\Http\Helpers\Helper::class,
Using the Helper as an alias, you can reference it in your blade template, example below:
{{Helper::formatDateToAgo ($notification->created_at)}}
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:
"autoload": {
"files": [
"app/Http/Helpers/helpers.php"
]
},
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:
use \App\Http\Controllers\ControllerName;
In my blade view (Laravel), I used below blade syntax (specify full path) to call my controller action.
{{ App\Http\Controllers\mynestedpageController::index() }}
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 the autoload
block of your composer.json in following way :
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Http/Helpers/helpers.php"
]
},
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.
public static function funtion_name($args) {}
Call.
\App\Http\Controllers\ControllerName::function_name($args)
If you don't like the very long code, you can just make it
ControllerName::function_name($args)
but don't forget to call it from the top of the view page.
use \App\Http\Controllers\ControllerName;