博主信息
博文 4
粉丝 0
评论 0
访问量 8629
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
laravel 初识路由
colonel的博客
原创
988人浏览过

路由文件名:routes.php   路径 app/Http/routes.php(此处以laravel 5.2 版本为例)

<?php

Route::get('/', function () {
   return view('welcome'); //  访问  127.0.0.1/你的框架文件名/public/hello”  路由显示视图‘welcome’
});

Route::get('hello',function(){
   return 'hello world';  //   访问  127.0.0.1/你的框架文件名/public/hello”          路由输出 ‘hello world’
});

//基础路由
Route::match(['get','post'],'nihao',function(){
   return '你好!'; // 访问“127.0.0.1/你的框架文件名/public/nihao”页面返回显示‘你好’
});

路由参数

//浏览器路径  127.0.0.1/你的框架文件夹名/public/user/name( name 就是你的参数)
Route::get('user/{name?}',function($name = '瓜皮'){
   return 'user-name-'.$name;
})->where('name','[A-Za-z]+');

//限制规则(正则):只有当name只由字母组成的时候此路由生效

//浏览器路径127.0.0.1/你的框架文件夹名/public/user/你的id
Route::get('user/{id}',function($id=null){
   return 'user-id-'.$id;
});

//浏览器路径127.0.0.1/你的框架文件夹名/public/user/你的id/你的name

Route::get('user/{id}/{name?}',function($id,$name = null){
   return 'user-id:'.$id.'&nbsp;user-name:'.$name;
})->where(['id' => '[0-9]+','name' => '[A-Za-z]+']);

//限制规则(正则):只有当id为数字、name只由字母组成的时候此路由生效



//路由别名

浏览器路径127.0.0.1/你的框架文件夹名/public/user/center

/*Route::get('user/center',['as'=>'center',function(){
   return route('center');  //浏览器返回别名为 ‘center’ 的路由路径
}]);

//路由群组,路由群组内部的路由均被添加一个上级(prefix 的值)


浏览器路径127.0.0.1/你的框架文件夹名/public/vip/user/center(left)

Route::group(['prefix' => 'vip'],function(){
    Route::get('user/center',['as'=>'center',function(){
        return route('center');
    }]);
    Route::get('user/left',['as'=>'left',function(){
        return route('left');
    }]);
});

路由输出试图

Route::get('view',function(){
   return view('welcome');//视图文件路径/resources/view/
});

路由连接控制器

Route::get('index','IndexController@index');
Route::get('index',['uses' =>'IndexController@index']);
//IndexController为控制器名  index 为方法名

Route::get('index',[
    'uses' => 'IndexController@index',
    'as'   => 'Index'
])
Route::get('index/{id}',['uses' =>'IndexController@index'])->where('id','[0-9]+');

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学