Monica
Monica

Reputation: 1534

same function with different return to Views on laravel

I am new to Laravel and I am doing the following, but I was wondering if there was a better way to do it.

Both do the same thing but return to a different view

    /**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    //Get all the services
    $franchises = Franchise::all();

    //Load the view and pass the services
    return View::make('franchises.index')->with('franchises', $franchises);
}

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function indexTwo()
{
    //Get all the services
    $franchises = Franchise::all();

    //Load the view and pass the services
    return View::make('admin.usertemplate')->with('franchises', $franchises);
}   

And this is my route.php

Route::get('admin/logout', array('as' => 'admin.logout', 'before' => 'user', 'uses' => 'AuthController@getLogout'));
Route::get('admin/login', array('as' => 'admin.login', 'before' => 'is_guest', 'uses' => 'AuthController@getLogin'));
Route::post('admin/login', array('as' => 'admin.login.post', 'before' => 'is_guest', 'uses' => 'AuthController@postLogin'));


//---- Backedn Index
Route::get('admin',array('as'=>'index','uses'=>'BackendController@getIndex'));

Upvotes: 0

Views: 1596

Answers (1)

Miroslav Trninic
Miroslav Trninic

Reputation: 3455

Your examples represents two controllers methods.In isolation, they don't do anything. They depends on Route (not provided) and Model(Franchise).

This can be improved in a number of ways, depending on your domain logic layer design.

You could for example do this:

return View::make("franchises.index",compact("franchises"));

or this:

return View::make("franchises.index",["franchise"=>$franchises]);

But it is all variation of same thing. Further it can be improved by applying repository pattern which gives you more flexibility when dealing with database. i.e. not depending on one ORM (eloquent).

As I sad, everything depends on your goals. Controller is just a door to you domain logic.

UPDATE TO YOUR UPDATE:

You can group your routes in one controller:

Route::resource("admin","AdminController");

And please move from laravel-3.

Upvotes: 1

Related Questions