如何在Laravel 5的视图中调用控制器函数 [英] How to call a controller function inside a view in laravel 5

查看:42
本文介绍了如何在Laravel 5的视图中调用控制器函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel 4中,我只是使用了一个函数

In laravel 4 i just used a function

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

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

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?

推荐答案

如果您有一个要在多个地方使用的函数,则应在助手文件中定义它,为此可以在app/Http中创建一个(可能是) /Helpers文件夹并将其命名为helpers.php,通过以下方式在composer.json的autorun块中提及此文件:

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 :

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/Http/Helpers/helpers.php"
    ]
},

运行composer dump-autoload,然后您可以从任何地方调用此函数,使其成为控制器视图或模型.

run composer dump-autoload, and then you may call this function from anywhere, let it be controller view or model.

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

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

打电话.

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

这篇关于如何在Laravel 5的视图中调用控制器函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆