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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35332784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:25:59  来源:igfitidea点击:

How to call a controller function inside a view in laravel 5

phplaravel-5

提问by Vishal

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). 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?

从我的 balde 模板(查看页面)调用控制器函数。现在我正在使用 Laravel 5 来做一个新项目,我尝试用这个方法从我的刀片模板调用控制器函数。但它不工作并显示一些错误。是否有任何方法可以从 Laravel 5 中的视图页面调用控制器函数?

回答by Mr. Engineer

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

回答by Khan Shahrukh

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 autoloadblock 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. Create.

或者如果您不需要放入助手。你可以简单地从它的控制器调用它。只需将其设为static function. 创建。

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;

回答by Acheme Paul

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 e.g

functionName 应该有 'static' 关键字,例如

Controller function:

控制器功能:

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

回答by Yesuus Yesuus

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;
 }

回答by Seyi Daniels

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)}}                                                

回答by Karthick Jayaraman

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() }}

回答by mbougarne

I like and favor Khan Shahrukhway, 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 methodDon't forget to add this line at the very top of your view:

如果您决定使用公共静态方法,请不要忘记在视图的最顶部添加这一行:

use \App\Http\Controllers\ControllerName;

回答by Luke Snowden

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

转储您的自动加载,您可以从应用程序内的任何位置调用这些函数

回答by Vijay Sai Chaudary

Actually below should work as per Laravel 5:
Usage: App::make(ControllerName)->functionName($parameters);
Example: App:make("TestController")->getUserInfo('user_id' => 9);

实际上下面应该按以下方式工作Laravel 5
用法:App::make(ControllerName)->functionName($parameters);
示例:App:make("TestController")->getUserInfo('user_id' => 9);

Please post the actual error you are getting!

请发布您遇到的实际错误!