php 如何在 Laravel 中设置默认控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14402967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:11:52  来源:igfitidea点击:

How to set the default controller in Laravel?

phplaravellaravel-routing

提问by DisgruntledGoat

I'm using the Laravel PHP framework and am wondering about a few things. The default application/routes.phpfile contains this:

我正在使用 Laravel PHP 框架并且想知道一些事情。默认application/routes.php文件包含以下内容:

Route::get('/', function()
{
    return View::make('home.index');
});

This just outputs the view, but how do I call a controller from there?

这只是输出视图,但如何从那里调用控制器?

I can delete the entire route above and replace it with Route::controller('home')which seems to use the home controller on the default URL (i.e. example.com/). But any other controller like Route::controller('article')doesn't work, only on example.com/article. How would I set the article controller as the default?

我可以删除上面的整个路由并将其替换为Route::controller('home')似乎在默认 URL 上使用 home 控制器的路由(即example.com/)。但是任何其他控制器Route::controller('article')都不起作用,只能在example.com/article. 我如何将文章控制器设置为默认值?

回答by Joseph Silber

Just pass the controller as a string, with @between the class name and the method name:

只需将控制器作为字符串传递,@在类名和方法名之间:

Route::get('/', 'article@index');

Read the docs(scroll to the example by the title Registering a route that points to a controller action).

阅读文档(通过标题注册指向控制器操作的路由滚动到示例)。

回答by Naveen

The "/" is special location and you can set it via Route::get('/','home@index').

/”是特殊位置,您可以通过Route::get('/','home@index').

For all other actions on home controller you will have urlssuch has "/home/action1" or "/home/action2".

对于家庭控制器上的所有其他操作,您将拥有带有“ ”或“ ”的网址/home/action1/home/action2

I am just trying to make you understand that there is no benefit and need of making any controller assign to "/".

我只是想让您了解将任何控制器分配给“ /”没有任何好处和需要。

I hope I am clear with my reply. Again this is not an attempted answer to your question but a suggestion for you if you are stuck with route handling. I was at the same stage where you are few days back :)

我希望我的回答很清楚。同样,这不是对您的问题的尝试回答,而是在您遇到路由处理问题时为您提供的建议。我和你几天前处于同一阶段:)