PHP Lavavel 使用控制器 传递变量 以及调用 视图模板

控制器第一次入门使用

位置: 在app/Http/Controllers 目录下创建
文件名格式:例如 UserController
路由调用格式:Route::get('user/tom','UserController');

控制器 第一次传变量入门

Route::get('user/show/{id}/{name}','UserController@show');
Route::get('user/show/{id}',UserController@show);
Route::get('user{id},'UserController@show'');

控制器第一次调用视图模板
view('');
return view('user.profile',['user']=>$id);
传变量到模板

web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/helloworld',function(){
    //return "Hello World! 这个是不使用模板 自定义字符串";
    return view('helloworld',['name'=>'Laravel Zero Basic Quickstart','title'=>'helloworld','records'=>0]);
}
);

Route::get('/bootstrap',function(){
    return view('bootstrap');
});

Route::get('user/show/{id}/{name}','UserController@show');
Route::get('user/show/{id}','UserController@list');
Route::get('user{id}','UserController@list');

UserController.php

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /***
    *为指定用户显示详情
    *@param int $id
    *@return Response
    *@author ebookApp
    **/
    public function show($id,$name)
    {
        //return "Hello,".$id.$name;
        //return     view('user.profile',['user'=>User::findOrFail($id)]);    
        return view('helloworld',['id'=>$id,'name'=>$name,'records'=>30]);
    }
    
    public function list($id)
    {
        return "123456789-".$id;
    }
}

helloworld.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>{{$name}}</title>
</head>
<body>
<div class="jumbotron text-center">
<h1>{{$name}}</h1>
<p>Video Course</p>
</div>
<div class="container">

<div class="row">
<div class="col-sm-8">
    <div class="card">
        <div class="card-header">Course List</div>
        <div class="card-body">Content
        @if($records ===1)
            I have one records
        @elseif($records>1)
            I have multiple records
        @else
            I don't have any records
        @endif

        <br/>
        @for($i=0;$i<10;$i++)
            The current value is {{$i}}<br/>
        @endfor
        </div>
    </div>
</div>

<div class="col-sm-4">
    <div class="card-header">Course Intro</div>
    <div class="card-body">Laravel Quickstart</div>
    <div class="card-footer">Footer</div>
</div>

</div>

</div><!--END container-->

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

 

posted @ 2018-04-16 16:27 kaixin110 阅读(...) 评论(...) 编辑 收藏