PHP Lavavel uses controllers to pass variables and call view templates

Getting started with the controller for the first time

Location: Create a file in the app/Http/Controllers directory.
Format: For example, UserController
route call format: Route::get('user/tom','UserController');

Getting started with passing variables for the first time in the controller

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

The first time the controller calls the view template
view('');
return view('user.profile',['user']=>$id);
pass variables to the template

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! This is a custom string without template"; 
    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
{
    /***
    *Display details for specified users
    *@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>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521144&siteId=291194637