0

我对 Laravel 真的很陌生,所以我有这个问题:

如果我在数据库中有 2 个表,比如说:位置和向量,并且我希望能够编辑/添加/更新/删除它们,我需要什么样的模型/控制器/视图结构?

我是否要为位置和矢量创建控制器?我应该创建一个设置控制器并只为位置和矢量创建模型吗?

4

2 回答 2

0

这完全取决于您,但是我将为每个模型创建一个模型,一个存储所有逻辑的存储库,可能会调用SettingsRepository.php它,然后将该存储库用于您需要使用该代码的任何地方。您还必须修改您的composer.json文件并确保您将存储库放入的文件夹位于该autoload部分中。

至于您的控制器,您可能只需要一个从存储库中获取数据并将其插入视图的控制器。当我想到设置时,我会想到您在应用程序的其他区域以及存储库中需要的设置,其他控制器可以轻松访问所有这些信息。

于 2013-11-01T13:20:30.323 回答
0

模型处理与数据库的直接交互。创建一个“位置”模型和一个“矢量”模型。当你这样做时,你将扩展 Eloquent 模型,你很快就会拥有一个知道如何添加、删除、更新、保存等的模型。 http://laravel.com/docs/eloquent

为您希望用户看到的每个页面创建一个视图。(您最终将创建主模板视图,但现在不要担心这个)。 http://laravel.com/docs/responses#views

控制器在视图和模型之间处理数据。Laravel 文档中有一个关于此的部分,但我没有足够的代表点来发布超过 2 个链接。

以最有意义的方式对控制器功能进行分组。如果您的所有网站路由都以“/settings”开头,那是一个危险信号,您可能只需要一个“设置”控制器。

这是您可能想要做的一个非常简化的示例。有很多不同的方法可以完成你想要的。这是一个例子。

// View that you see when you go to www.yoursite.com/position/create
// [stored in the "views" folder, named create-position.php]
<html><body>
<form method="post" action="/position/create">
    <input type="text" name="x_coord">
    <input type="text" name="y_coord">
    <input type="submit">
</form>
</body></html>


// Routing [in your "routes.php" file]
// register your controller as www.yoursite.com/position
// any public method will be avaliable at www.yoursite.com/methodname
Route::controller('position', 'PositionController');


// Your Position Controller
// [stored in the "controllers" folder, in "PositionController.php" file
// the "get" "post" etc at the beginning of a method name is the HTTP verb used to access that method.
class PositionController extends BaseController {

    public function getCreate()
    {
        return View::make('create-position');
    }

    public function postCreate()
    {
        $newPosition = Position::create(array(
            'x_coord' => Input::get('x_coord'),
            'y_coord' => Input::get('y_coord')
        ));

        return Redirect::to('the route that shows the newly created position');
    }

}

// Your Position Model
class Position extends Eloquent {

    // the base class "Eloquent" does a lot of the heavy lifting here.

    // just tell it which columns you want to be able to mass-assign
    protected $fillable = array('x_coord', 'y_coord');

    // yes, nothing else required, it already knows how to handle data

}
于 2013-11-03T21:55:19.537 回答