Controller first time to get started using
Location: Created under the App/http/controllers directory
File name format: for example Usercontroller
Routing call format: Route::get (' User/tom ', ' Usercontroller ');
Introduction to controller First pass variable
Route::get (' User/show/{id}/{name} ', ' [email protected] ');
Route::get (' User/show/{id} ', [email protected]);
Route::get (' User{id}, ' [email protected] ');
The controller invokes the view template for the first time
View (');
Return view (' User.profile ', [' User ']=> $id);
Transfer variables to templates
web.php
<?PHP/*|--------------------------------------------------------------------------| Web routes|--------------------------------------------------------------------------| | Here's where you can register the Web routes for your application. these| Routes is loaded by the Routeserviceprovider within a group which| Contains the "Web" middleware group. Now create something great!|*/Route:: Get ('/',function () { returnView (' Welcome ');}); Route:: Get ('/helloworld ',function(){ //return "Hello world! This is a custom string that does not use a template "; returnView (' HelloWorld ', [' name ' = ' = ' Laravel Zero Basic Quickstart ', ' title ' = ' HelloWorld ', ' Records ' =>0]);}); Route:: Get ('/bootstrap ',function(){ returnView (' Bootstrap ');}); Route:: Get (' User/show/{id}/{name} ', ' [email protected] '); Route:: Get (' User/show/{id} ', ' [email protected] '); Route:: Get (' User{id} ', ' [email protected] ');
usercontroller.php
<?phpnamespace app\http\controllers; UseApp\user; Useilluminate\http\request;classUsercontrollerextendscontroller{/** * Show details for specified users * @param int $id * @return Response * @author Ebookapp **/ Public functionShow$id,$name) { //return "Hello,". $id. $name; Return view (' User.profile ', [' User ' =>user::findorfail ($id)]); returnView (' HelloWorld ', [' id ' = =$id, ' name ' = =$name, ' Records ' =>30]); } Public function List($id) { return"123456789-".$id; }}
helloworld.blade.php
<! DOCTYPE html>$name}}</title>class= "Jumbotron text-center" >$name}}class= "Container" ><divclass= "Row" ><divclass= "Col-sm-8" > <divclass= "card" > <divclass= "Card-header" >courseList</div> <divclass= "Card-body" >Content @if($records===1) I have one records @ElseIf($records>1) I have multiple records @ElseI Don‘T has 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= "c Ard-header ">course intro</div> <div class=" Card-body ">laravel quickstart</div> <div class=" c Ard-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>
PHP lavavel using controllers to pass variables and invoke view templates