dongtaotao19830418 2014-02-14 19:33
浏览 48

相同的功能,不同的返回视图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'));
  • 写回答

1条回答 默认 最新

  • dongsu2807 2014-02-14 19:46
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。