php - Laravel关于路由跳转的问题
PHPz
PHPz 2017-04-10 16:46:41
[PHP讨论组]

在laravel官方文档中有这么一个路由:

Route::group(['domain' => '{account}.myapp.com'], function () {
    Route::get('user/{id}', function ($account, $id) {
        // 这里如何写,才能跳转到控制器呢?
    });
});

注释的地方怎么写才能跳转到控制器呢,其实也就是说路由里面的function中,如何跳转到控制器,例如:跳转到MainController的index方法

PHPz
PHPz

学习是最好的投资!

全部回复(2)
ringa_lee
Route::group(['domain' => '{account}.myapp.com'], function () {
    Route::get('user/{id}', 'MainController@index');
});

跳转到MainController的index方法。

index方法似乎应该写成function ($account,$id){}

天蓬老师

可以用make方法:

Route::group(['domain' => '{account}.myapp.com'], function () {
    Route::get('user/{id}', function ($account, $id) {
          $app = app(); // 初始化app
          $controller = $app->make('MyController'); // 调用控制器
          return $controller->callAction('index', $parameters); //调用控制其方法并传参
    });
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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