Search code examples
phplaravellaravel-3

Laravel: How to redirect a request to controller function and use View::make at the same time?


**Route**
Route::get('admin', function()
{
     return View::make('theme-admin.main');
});

**Controller**
class Admin_Controller extends Base_Controller {

public function action_index()
{
    echo __FUNCTION__;
}

If I forward request to controller, then I have to define View::make in every function in controller. If I don't forward it, action function doesn't work.

Should I just forward requests to controller and use View::make inside action functions or there are better alternatives?


Solution

  • Actually isn't necessary to define View::make in every function of your controllers.

    You can, for example, execute an action and then redirect to another action, that could View::make.

    Let's say you want to create an user and then show its profile, in a RESTful way. You could do:

    # POST /users
    public function user_create()
    {
        $user = User::create(...);
    
        // after you have created the user, redirect to its profile
        return Redirect::to_action('users@show', array($user->id));
    
        // you don't render a view here!
    }
    
    
    # GET /users/1
    public function get_show($id)
    {
        return View::make('user.show');
    }