Php 同一路线,不同的控制器
我想有一般的主页 为登录用户提供不同的主页Php 同一路线,不同的控制器,php,controller,routing,laravel,laravel-4,Php,Controller,Routing,Laravel,Laravel 4,我想有一般的主页 为登录用户提供不同的主页 我在谷歌上搜索了很多,但我找不到在if语句中放什么 我试过这样的方法: Route::get('/', array('as'=>'home', function(){ if (!Auth::check()) { Route::get('/', array('uses'=>'homecontroller@index')); } else{ Route::get('/', array('u
我在谷歌上搜索了很多,但我找不到在if语句中放什么 我试过这样的方法:
Route::get('/', array('as'=>'home', function(){
if (!Auth::check()) {
Route::get('/', array('uses'=>'homecontroller@index'));
}
else{
Route::get('/', array('uses'=>'usercontroller@home'));
}
}));
我还尝试了以下方法:
return Controller::call('homecontroller@index');
Route::get('/', array('as'=>'home', function(){
if (!Auth::check()) {
Redirect::to('home/index'));
}
else{
Redirect::to('user/index'));
}
}));
但它似乎不是为拉威尔4而设计的我尝试了很多其他的东西,所以我认为这更多的是一个误解问题 如果你有任何线索
感谢您的帮助我能想到的最简单的解决方案是:
<?php
$uses = 'HomeController@index';
if( ! Auth::check())
{
$uses = 'HomeController@home';
}
Route::get('/', array(
'as'=>'home'
,'uses'=> $uses
));
您应该尝试以下方法:
return Controller::call('homecontroller@index');
Route::get('/', array('as'=>'home', function(){
if (!Auth::check()) {
Redirect::to('home/index'));
}
else{
Redirect::to('user/index'));
}
}));
因此,您基本上是基于身份验证检查重定向用户,而不是定义额外的路由
或者使用路由过滤器
Route::filter('authenticate', function()
{
if (!Auth::check())
{
return Redirect::to('home/index');
}
});
Route::get('home', array('before' => 'authenticate', function()
{
Redirect::to('user/index');
}));
好的,在这个平台和其他论坛上讨论过之后,我带着一个紧凑的解决方案回来了
Route::get('/', array('as'=>'home', 'uses'=> (Auth::check()) ? "usercontroller@home" : "homecontroller@index" ));
谢谢andreyco,但这不是我想要的。正如您在我的示例中所看到的,我希望路由到另一个控制器,因此我更喜欢在路由文件中进行测试我在路由关闭中遇到了问题,您在执行测试之前更改了透视图。。。你太棒了!它可以很好地工作,但这样的编程逻辑会将routes.php文件弄得乱七八糟,使其更难阅读。在我看来,使用过滤器是一个更好的选择。我同意,但我现在找不到一个解决方案,使用过滤器并停留在root“/”@GladToHelp,那么您如何使用过滤器呢?