简体   繁体   English

相同的功能,但对laravel视图的返回不同

[英]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. 我是Laravel的新手,我正在做以下工作,但我想知道是否有更好的方法可以做到。

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.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'));

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. 此外,还可以通过应用存储库模式进行改进,该模式在处理数据库时为您提供了更大的灵活性。 ie not depending on one ORM (eloquent). 即不依赖于一个ORM(雄辩)。

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. 并且请从laravel-3移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM