简体   繁体   English

如何在 Laravel 5 的视图中调用控制器函数

[英]How to call a controller function inside a view in laravel 5

In laravel 4 i just used a function在 laravel 4 中,我只使用了一个函数

$varbl = App::make("ControllerName")->FunctionName($params);

to call a controller function from a my balde template(view page).从我的 balde 模板(查看页面)调用控制器函数。 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.现在我正在使用 Laravel 5 来做一个新项目,我尝试用这个方法从我的刀片模板调用控制器函数。但它不工作并显示一些错误。 Is there any method to call a controller function from a view page in Laravel 5?是否有任何方法可以从 Laravel 5 中的视图页面调用控制器函数?

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参考这个: 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 the autoload block of your composer.json in following way :如果您有一个在多个地方使用的函数,您应该在 helpers 文件中定义它,为此在 app/Http/Helpers 文件夹中创建一个(可能是)并将其命名为 helpers.php,在autoload块中提及此文件您的 composer.json 以下列方式:

"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.运行 composer dump-autoload,然后你可以从任何地方调用这个函数,让它成为控制器视图或模型。

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 .只需将其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;

In laravel 5, you can do it like so在laravel 5中,你可以这样做

In your view:在您看来:

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

The functionName should have the 'static' keyword eg functionName 应该有 'static' 关键字,例如

Controller function:控制器功能:

public static function functionName() {
return "Hello World!";
}

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你实际上可以在你的刀片模板中调用一个类、帮助类或任何声明的类,但把它放在 config 文件夹中 app.php 的别名数组中

        'Helper' =>   App\Http\Helpers\Helper::class,

Using the Helper as an alias, you can reference it in your blade template, example below:使用 Helper 作为别名,您可以在刀片模板中引用它,示例如下:

        {{Helper::formatDateToAgo ($notification->created_at)}}                                                

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.这里 AdminAfterAuth 是控制器类名,globAdmin 是方法名。

Now in your controller declare the method statically.现在在您的控制器中静态声明该方法。

public static function globAdmin(){
 $admin_val = AdminLogin::where('id',session('admin_id'))->get();
 return $admin_val;
 }

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:我喜欢并喜欢Khan Shahrukh方式,最好创建一个包含所有功能的帮助文件,然后将其添加到您的 composer.json 文件中:

"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.您可以选择适合您的路径,然后 dump-autoload composer 使其包含新文件。

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.在我的刀片视图 (Laravel) 中,我使用以下刀片语法(指定完整路径)来调用我的控制器操作。

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

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.然后你就完成了。

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将助手文件添加到 compose.json 文件的第二个选项

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

dump your autoload and you can call these functions from anywhere inside your application转储您的自动加载,您可以从应用程序内的任何位置调用这些函数

Actually below should work as per Laravel 5 :实际上下面应该按照Laravel 5
Usage: App::make(ControllerName)->functionName($parameters);用法: App::make(ControllerName)->functionName($parameters);
Example: App:make("TestController")->getUserInfo('user_id' => 9);示例: App:make("TestController")->getUserInfo('user_id' => 9);

Please post the actual error you are getting!请发布您遇到的实际错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM