Blogger Information
Blog 55
fans 3
comment 0
visits 54655
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Laravel路由、控制器和模板引擎语法
王佳祥
Original
801 people have browsed it

Laravel路由、控制器和模板引擎语法

一、控制器创建

  • 手动方式:

app\Http\Controllers目录下创建自定义的控制器

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. Class Home extends Controller{
  5. public function index(){
  6. echo '这是home中的index方法';
  7. }
  8. public function php(Request $request){
  9. echo '这是home中的php方法';
  10. $data['name'] = $request->name;
  11. return view('test',$data);
  12. }
  13. }

控制器名与类名一致

  • 命令行方式:

打开命令行,进入本地的laravel的根目录输入以下命令:

php artisan make:controller 控制器名

二、控制器向视图传值

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. Class Home extends Controller{
  5. public function index(){
  6. echo '这是home中的index方法';
  7. }
  8. public function php(Request $request){
  9. echo '这是home中的php方法';
  10. $data['name'] = $request->name;
  11. return view('test',$data);
  12. }
  13. }

视图:test.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>测试视图</title>
  7. </head>
  8. <body>
  9. <a href="http://www.php.cn"><h2>php中文网{{$name}}</h2></a>
  10. </body>
  11. </html>

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/', function () {
  4. return view('welcome');
  5. });
  6. Route::get('/home.hop','Home@php');


三、blade语法:if 、witch 和 变量

  • 变量:

视图:test.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>测试视图</title>
  7. </head>
  8. <body>
  9. <div>用户名:{{$username}}</div>
  10. <div>账号:{{$zh}}</div>
  11. <div>密码:{{$mm}}</div>
  12. <div>地址:{{$dz}}</div>
  13. <div>国家:{!!$gj!!}</div>
  14. <a href="http://www.php.cn"><h2>php中文网{{$name}}</h2></a>
  15. </body>
  16. </html>

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. Class Home extends Controller{
  5. public function index(){
  6. echo '这是home中的index方法';
  7. }
  8. public function php(Request $request){
  9. echo '这是home中的php方法';
  10. $data['name'] = $request->name;
  11. $data['username'] = '<script>alert("admin");</script>';
  12. $data['zh'] = '156320';
  13. $data['mm'] = 'dasnfld';
  14. $data['dz'] = '山西省临汾市';
  15. $data['gj'] = '<sapn style="color:red">中国</span>';
  16. return view('test',$data);
  17. }
  18. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/', function () {
  4. return view('welcome');
  5. });
  6. Route::get('/home.hop','Home@php');


  • if:

如果用户名存在就显示出来,不存在就显示登录

视图:test.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>测试视图</title>
  7. </head>
  8. <body>
  9. <div>
  10. @if($username)
  11. 用户名:{{$username}}
  12. @else
  13. <a href="">登录</a>
  14. @endif
  15. </div>
  16. <div>账号:{{$zh}}</div>
  17. <div>密码:{{$mm}}</div>
  18. <div>地址:{{$dz}}</div>
  19. <div>国家:{!!$gj!!}</div>
  20. <a href="http://www.php.cn"><h2>php中文网{{$name}}</h2></a>
  21. </body>
  22. </html>

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. Class Home extends Controller{
  5. public function index(){
  6. echo '这是home中的index方法';
  7. }
  8. public function php(Request $request){
  9. echo '这是home中的php方法';
  10. $data['name'] = $request->name;
  11. //不存在就显示登录
  12. //$data['username'] = '';
  13. //存在就显示用户名
  14. $data['username'] = 'admin';
  15. $data['zh'] = '156320';
  16. $data['mm'] = 'dasnfld';
  17. $data['dz'] = '山西省临汾市';
  18. $data['gj'] = '<sapn style="color:red">中国</span>';
  19. return view('test',$data);
  20. }
  21. }


  • witch:

年龄匹配到17,显示未成年;匹配到18显示刚成年;匹配到30显示老男人

视图:test.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>测试视图</title>
  7. </head>
  8. <body>
  9. <div>
  10. @if($username)
  11. 用户名:{{$username}}
  12. @else
  13. <a href="">登录</a>
  14. @endif
  15. </div>
  16. <div>账号:{{$zh}}</div>
  17. <div>
  18. @switch($age)
  19. @case(17)
  20. <div style="color:red;">未成年</div>
  21. @break
  22. @case(18)
  23. <div style="color:green;">刚成年</div>
  24. @break
  25. @case(30)
  26. <div style="color:yellow;">老男人</div>
  27. @break
  28. @default
  29. <div>不知道</div>
  30. @endswitch
  31. </div>
  32. <div>地址:{{$dz}}</div>
  33. <div>国家:{!!$gj!!}</div>
  34. <a href="http://www.php.cn"><h2>php中文网{{$name}}</h2></a>
  35. </body>
  36. </html>

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. Class Home extends Controller{
  5. public function index(){
  6. echo '这是home中的index方法';
  7. }
  8. public function php(Request $request){
  9. echo '这是home中的php方法';
  10. $data['name'] = $request->name;
  11. $data['username'] = 'admin';
  12. $data['zh'] = '156320';
  13. $data['age'] = '18';
  14. $data['dz'] = '山西省临汾市';
  15. $data['gj'] = '<sapn style="color:red">中国</span>';
  16. return view('test',$data);
  17. }
  18. }


Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:相当的用心
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post