laravel如何向视图传递值

1.定义路由

 Route::get('demo','DemoController@demo');

2.定义控制器(内with();方法就是定义传递的值 key=>value)=>"with什么什么在一起;跟; 有; 具有; 带有; 用; 使用; 以; 借;的意思(翻译)"

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    //
    public function demo(){
        return view('demo')->with('name','我叫张三');
    }
}

3.前端blade模板调用

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>向视图传递值</title>
</head>
<body>
<h2 style="font-family: "Hiragino Sans GB", "微软雅黑", "华文细黑", "STHeiti", "Microsoft YaHei", SimHei, "Helvetica Neue", Helvetica, Arial, sans-serif">向视图传递值</h2>
<script>
    window.onload=function () {
        alert('{{$name}}');
    }
</script>
</body>
</html>

4.显示视图如下(ps:需要注意的是 $name在前端是变量一定要注意

posted @ 2020-02-19 10:24  以己为镜  阅读(651)  评论(0编辑  收藏  举报