简体   繁体   English

如何在 Laravel 5 中从视图中调用控制器

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

I am using Laravel 5 in which i have to call a controller from view blade.我正在使用 Laravel 5,其中我必须从视图刀片调用控制器。 But it showing me parse error.但它显示我解析错误。 Please find my code.请找到我的代码。

Controller name:ReportController(path=app/Http/Controllers/Admin/ReportController)控制器名称:ReportController(path=app/Http/Controllers/Admin/ReportController)

<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
//send email use helper
use App\Helpers\MyHelperFunction;

 public static function my($args){
        // do your stuff or return something.
        echo $args."I am called on view.";
    }
?>

View:customerorders.blade.php查看:customerorders.blade.php

<?php 
use App\Http\Controllers\Admin\ReportController;
     echo ReportController::my('hello'); 
?>

I facing this error:我面临这个错误:

Method Illuminate\\View\\View::__toString() must not throw an exception, caught ErrorException: Parse error: syntax error, unexpected 'use' (T_USE) (View:/opt/lampp/htdocs/buddyiq_dev/resources/views/Admin/reports/customerorders.blade.php)方法 Illuminate\\View\\View::__toString() 不能抛出异常,捕获 ErrorException: Parse error: syntax error, unexpected 'use' (T_USE) (View:/opt/lampp/htdocs/buddyiq_dev/resources/views/Admin /reports/customerorders.blade.php)

I refer above code from below stack url.我从下面的堆栈 url 中引用上面的代码。

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

Please help me to resolve this issue.请帮我解决这个问题。

You could try using ajax to call to function in a controller, then get the respones and push it to view.您可以尝试使用 ajax 调用控制器中的函数,然后获取响应并将其推送到查看。 About respones, remember to put return in your function.关于响应,记得把return放在你的函数中。

If your motivation for calling a controller method is just to output a string a controller may be inappropriate.如果您调用控制器方法的动机只是输出字符串,则控制器可能不合适。 Controllers bind models and views together.控制器将模型和视图绑定在一起。

在此处输入图片说明

image source: Basic Laravel 5 MVC 图片来源:基本 Laravel 5 MVC

Views really shouldn't be aware of controllers methods.视图真的不应该知道控制器方法。 You might be better off investigating view helper methods .您最好研究一下视图助手方法

This is also answered in the question you link to这也在您链接到的问题中得到了回答

If you have a function which is being used at multiple places you should define it in helpers file如果你有一个在多个地方使用的函数,你应该在 helpers 文件中定义它

This Q/A is helpful Best practices for custom helpers on Laravel 5这个 Q/A 很有帮助Laravel 5 上自定义助手的最佳实践

So I just tried this on one of my Laravel setups.所以我只是在我的 Laravel 设置之一上尝试了这个。 I'm not sure if it's the best approach but it works.我不确定这是否是最好的方法,但它有效。

Declare your function as将您的函数声明为

public static function my($args)
{
       echo $args."I am called on view.";
}

which you already did.你已经这样做了。 then call it from the view as {{App\\Http\\Controllers\\Admin\\ReportController::my($args)}}然后从视图中调用它作为{{App\\Http\\Controllers\\Admin\\ReportController::my($args)}}

Use the absolute path instead relative path, you can use this as like below,使用绝对路径而不是相对路径,您可以像下面这样使用它,

 \App\Http\Controllers\Admin\ReportController

but you have used App\\Http\\Controllers\\Admin\\ReportController , please change it in every places.但是你已经使用了App\\Http\\Controllers\\Admin\\ReportController ,请在每个地方更改它。

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

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