本文实例讲述了Laravel框架中的路由和控制器操作。分享给大家供大家参考,具体如下:
路由
//get请求
Route::get('hello1',function(){
return 'hello world';
})
//post请求
Route::post('hello2',function(){
return 'hello world';
})
//get、post请求
//match用来匹配指定请求的类型
Route::match(['get','post'],'mulity',function(){
return 'mulity request';
})
//any匹配所有类型的请求
Route::any('mulity2',function(){
return 'mulity2 request';
})
Route::get('user/{id}', function ($id) {
return 'User '.$id;});
Route::get(‘user/{name?}',function($name = null){
Return ‘name'.$name});
Route::get('user/{name}', function ($name) {
//})- where('name', '[A-Za-z]+');
Route::get('user/{id}', function ($id) {
//})- where('id', '[0-9]+');
Route::get('user/{id}/{name}', function ($id, $name) {
//})- where(['id' = '[0-9]+', 'name' = '[a-z]+']);
Route::get('user/profile', ['as' = 'profile', function () {
//}]);
//路由前缀
Route::group(['prefix' = 'admin'], function () {
Route::get('users', function () {
// Matches The "/admin/users" URL });});
Route::get('/', function () {
return view('welcome');
});
控制器
<?php
//使用命名空间
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* 显示指定用户的个人信息
*
* @param int $id
* @return Response
*/
public function showProfile($id)
{
return view('user.profile', ['user' = User::findOrFail($id)]);
}
}
//@后面内容为所要访问的方法
Route::get('foo', 'Photos\AdminController@method');
//也可以指定控制器路由的名称
Route::get('foo', ['uses' = 'FooController@method', 'as' = 'name']);
//通过助手方法来生成ur
$url = route('name');
//传参$id
Route::get('user/{id}',['uses'= 'MemberController@info'])- where('id', '[0-9]+');
更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。