22

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?

Vishal
  • 552
  • 1
  • 6
  • 20

10 Answers10

67

Just try this in your view :

{{ ControllerName::Functionname($params); }}

OR

<?php echo ControllerName::Functionname($params);?>

Refer this : http://laravel.io/forum/03-06-2014-what-is-the-proper-way-to-call-controllers-from-the-view?page=1

Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
40

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;
Tobias Feil
  • 2,399
  • 3
  • 25
  • 41
Khan Shahrukh
  • 6,109
  • 4
  • 33
  • 43
21

In laravel 5, you can do it like so

In your view:

<?php use App\Http\Controllers\ControllerName;
echo ControllerName::functionName(); ?>

The functionName should have the 'static' keyword e.g

Controller function:

public static function functionName() {
return "Hello World!";
}
Acheme Paul
  • 1,194
  • 15
  • 19
6

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)}}                                                
Seyi Daniels
  • 190
  • 2
  • 8
4

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.

use App\Http\Controllers\AdminAfterAuth;
$admin_dtls = AdminAfterAuth::globAdmin();

Here AdminAfterAuth is the controller class name and globAdmin is the method name.

Now in your controller declare the method statically.

public static function globAdmin(){
 $admin_val = AdminLogin::where('id',session('admin_id'))->get();
 return $admin_val;
 }
Flexo
  • 87,323
  • 22
  • 191
  • 272
4

In view call the function like this:

@php 

use App\Http\Controllers\ControllerName; 
$var = ControllerName::FunctionName();

@endphp

But if the function name in the controller is as:

public function FunctionName(){

return something;

}

Then an error will be shown:

Non-static method App\Http\Controllers\ControllerName::FunctionName() should not be called statically(...)

So to solve this problem "non-static" you have to change your function like this:

public static function FunctionName(){
    
return something;
    
}

Then you are all done.

Tahasin
  • 338
  • 1
  • 11
  • In recent version of Laravel (9.x), I think `use` keyword in view file throws an error. But `App\Http\Controllers\ControllerName::FunctionName();` works fine. – Tariqul Islam Nov 10 '22 at 17:52
  • No. We are still using this method in our laravel 9.x projects. – Tahasin Nov 11 '22 at 09:39
  • Maybe I did something wrong while I was working on this thing. Thanks, for assuring us that it still works. Anyways, that one-line solution seems more appealing to me unless I need multiple static method call. – Tariqul Islam Nov 12 '22 at 10:24
2

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;
mbougarne
  • 91
  • 1
  • 4
2

In my blade view (Laravel), I used below blade syntax (specify full path) to call my controller action.

{{ App\Http\Controllers\mynestedpageController::index() }}

1

Controllers methods are not supposed to be called from the view. Best options are to call the method from the method which is returning the view object, which contains the output which you then can echo in the view;

    public function bar() {
        $foo = $this->foo();
        return view( 'my.view', compact( 'foo' ) );
    }

    protected method foo()
    {
        return 'foobar';
    }

second option to add a helpers file to the compose.json file

"autoload": {
    "files": [
        "app/helpers.php"
    ]
},

dump your autoload and you can call these functions from anywhere inside your application

Luke Snowden
  • 4,056
  • 2
  • 37
  • 70
-2

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!