Home > Backend Development > PHP Tutorial > Laravel关于路由跳转的问题

Laravel关于路由跳转的问题

WBOY
Release: 2016-06-06 20:18:29
Original
1350 people have browsed it

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

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

</code>
Copy after login
Copy after login

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

回复内容:

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

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

</code>
Copy after login
Copy after login

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

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

跳转到MainController的index方法。

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

可以用make方法:

<code>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); //调用控制其方法并传参
    });
});
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template