2013-04-07 27 views
1
**Route** 
Route::get('admin', function() 
{ 
    return View::make('theme-admin.main'); 
}); 

**Controller** 
class Admin_Controller extends Base_Controller { 

public function action_index() 
{ 
    echo __FUNCTION__; 
} 

如果我將請求轉發給控制器,那麼我必須在控制器中的每個函數中定義View::make。如果我不轉發,action function不起作用。Laravel:如何重定向到控制器的功能和使用視圖的請求::使在同一時間?

我是不是應該把請求轉發給控制器,並使用View::make內動作的功能或有更好的選擇?

回答

1

實際上沒有必要在控制器的每個功能中定義View::make

例如,您可以執行一個操作,然後重定向到另一個操作,該操作可以是View::make

假設您想創建一個用戶,然後以REST方式顯示其用戶檔案。你可以這樣做:

# POST /users 
public function user_create() 
{ 
    $user = User::create(...); 

    // after you have created the user, redirect to its profile 
    return Redirect::to_action('[email protected]', array($user->id)); 

    // you don't render a view here! 
} 


# GET /users/1 
public function get_show($id) 
{ 
    return View::make('user.show'); 
} 
+0

而不是重定向:: to_action它應該是Redirect :: action,至少在laravel 4.2中。 – Lucia 2015-01-05 19:39:54

0

,你可以這樣調用

$app = app(); 
$controller = $app->make('App\Http\Controllers\EntryController'); 
return $controller->callAction('getEntry', $parameters = array()); 

控制器功能,或者你可以簡單地將請求調度到另一個控制器網址

$request = \Request::create(route("entryPiont"), 'POST', array())); 
return \Route::dispatch($request); 
相關問題