laravel中的路由

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cofecode/article/details/82810215

路由

5.2 路由位置 app/http/routes.php

5.4 已经在根目录下 routes

Route类,使用静态方法

1.回调函数写法

第二个参数,回调函数,表示这个路由做的实际操作。

Route::get('/', function () {
    return view('welcome');
});

2.写控制器和方法

注意:这里第二个参数也是个字符串

Route::get('/', '[控制器]@[行为]');

例如:

Route::get('/user', 'indexCo@cc');

访问user的时候,调用indexCo控制器下面的cc方法

还可以写带参数

Route::get('user/{id}', function ($id) {
    return 'user'.$id;
});

访问http://www.cc.com/user/10

页面上就长这样

user10

多个参数情况

Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    echo $postId . $commentId;
});
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    echo $postId .'|' . $commentId;
});

访问这个页面的时候。

http://www.cc.com/posts/30/comments/45

得到30|45

猜你喜欢

转载自blog.csdn.net/cofecode/article/details/82810215