Laravel中路由基础知识点总结

Laravel中路由基础知识点总结

    在laravel5中,通常在 app/Http/routes.php 中定义应用中的大多数路由,这个文件加载了App\Providers\RouteServiceProvider 类。 大多数基本的 Laravel 路由都只接受一个 URI 和 一个闭包(Closure)参数;

    下面是一些基本路由使用方法与解释。

<?php

 

//当打开http://localhost:8080时显示/resources/views/vender/welcome.php网页的内容;get表示通过get方法;

Route::get('/', function () {

   return view('welcome');

});

 

Blade::setRawTags('{{', '}}');

 

//当打开地址为http://localhost:8080/music时,显示function()的内容,即  音乐列表…;

Route::get('music',function(){

   return '音乐列表...';

});

 

//Route::post('movie',function(){

//   return '发布电影...';

//});

 

正则表达式:

//http://localhost:8080/movie/12345显示  电影列表12345

Route::get('movie/{movie_id}',function($movie_id){

   return '电影列表'.$movie_id;

})

//->where('movie_id','[0-9]+'); //表示匹配数字,/movie/后只能是任意数字

->where('movie_id','[a-z]+');  //表示匹配字母,/movie/后只能是任意字母

 

在路由中将数据指定到视图

//传递数据到视图里,方法有几种

Route::get('movie',function(){

// ①  returnview('movie.index')->with('user','Edom'); //movie是文件夹,index表示index.php或者index.blade.php;

 // ②   returnview('movie.index')->withUser('Edom')->withEmail('edom@163.com');

   ③  $data = array(

       'user' => 'Edom',

       'email' => 'edom_Huang@163.com'

   );

   $data_block = array(

       'block_title' => '电影排行榜'

   );

   returnview('movie.index',$data)->nest('boxoffice','movie.block.boxoffice',$data_block);//把子视图嵌入到视图里面;nest方法三个参数的意义:①自定义名字②所要导入的php文件存放位置③数据

});

 

posted @ 2016-12-05 13:27  PG30吕润军的博客  阅读(246)  评论(0编辑  收藏  举报