search

Home  >  Q&A  >  body text

php - Laravel关于路由跳转的问题

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

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

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

PHPzPHPz2902 days ago400

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-10 16:48:41

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

    跳转到MainController的index方法。

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-10 16:48:41

    可以用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); //调用控制其方法并传参
        });
    });
    

    reply
    0
  • Cancelreply