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


你想要使用的控制器方法可以像下面这样添加到视图中:

<?php echo App\Http\Controllers\StudentController::test(); ?>

这里 App\Http\Controllers\StudentController 是控制器,test() 是我们想要调用的方法。

示例 1

视图文件是 student.blade.php。在这里,我们调用控制器 **studentController** 和方法 **test()**。方法 **test()** 在 studentController 中定义,如下所示:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; use URL; use Illuminate\Support\Facades\App; class StudentController extends Controller { public function index() { return view('student'); } public function validateform(Request $request) { } public function test() { echo "Testing controller function in view"; } }

HTML 文件

<html> <head> <title>Student Form </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student')); echo App\Http\Controllers\StudentController::test(); ?> <table border = '1'> <tr> <td align = 'center' colspan = '2'>Student Registration</td> </tr> <tr> <td>Name</td> <td><?php echo Form::text('name'); ?></td> </tr> <tr> <td>Email</td> <td><?php echo Form::text('email'); ?></td> </tr> <tr> <td>Age</td> <td><?php echo Form::text('age'); ?></td> </tr> <tr> <td>Address <td><?php echo Form::text('address'); ?></td> </tr> <tr> <td align = 'center' colspan = '2'><?php echo Form::submit('submit'); ?></td> </tr> </table> <?php echo Form::close(); ?> </body> </html>

输出

以上代码的输出为:

在上面的输出中,我们可以看到调用了 test() 方法,并且我们输出的消息显示给用户。

在视图中使用控制器的另一种方法如下:

示例 2

您可以使用以下代码在视图中使用控制器。

<?php use App\Http\Controllers\StudentController; StudentController::test(); ?>

添加以上代码后,视图如下所示:

<html> <head> <title>Student Form</title> </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student')); ?> <?php use App\Http\Controllers\StudentController; StudentController::test(); ?> <table border = '1'> <tr> <td align = 'center' colspan = '2'>Student Registration</td> </tr> <tr> <td>Name</td> <td><?php echo Form::text('name'); ?></td> </tr> <tr> <td>Email</td> <td><?php echo Form::text('email'); ?></td> </tr> <tr> <td>Age</td> <td><?php echo Form::text('age'); ?></td> </tr> <tr> <td>Address</td> <td><?php echo Form::text('address'); ?></td> </tr> <tr> <td align = 'center' colspan = '2'><?php echo Form::submit('submit'); ?></td> </tr> </table> <?php echo Form::close(); ?> </body> </html>

输出

以上代码的输出如下:

示例 3

您也可以通过在 blade 模板中将其括在花括号内来使用控制器,如下所示:

{{ App\Http\Controllers\StudentController::test() }}

在视图中,它将如下所示:

<html> <head> <title>Student Form</title> </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student')); ?> {{ App\Http\Controllers\StudentController::test() }} <table border = '1'> <tr> <td align = 'center' colspan = '2'>Student Registration</td> </tr> <tr> <td>Name</td> <td><?php echo Form::text('name'); ?></td> </tr> <tr> <td>Email</td> <td><?php echo Form::text('email'); ?></td> </tr> <tr> <td>Age</td> <td><?php echo Form::text('age'); ?></td> </tr> <tr> <td>Address <td><?php echo Form::text('address'); ?></td> </tr> <tr> <td align = 'center' colspan = '2'><?php echo Form::submit('submit'); ?></td> </tr> </table> <?php echo Form::close(); ?> </body> </html>

控制器如下所示:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; //use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; use URL; use Illuminate\Support\Facades\App; class StudentController extends Controller{ public function index() { return view('student'); } public function validateform(Request $request) { $data = file_get_contents('php://input'); print_r($data); } public function test() { echo "Testing controller function in view"; } }

输出

以上代码的输出为:

更新于:2022年8月30日

4K+ 次浏览

开启您的 职业生涯

完成课程获得认证

开始学习
广告