我真正的新Laravel,所以我有这样的疑问:Laravel 4设置控制器
如果我在数据库中有2个表,说:立场和载体,我希望能够 编辑/添加/更新/删除它们,我需要什么样的模型/控制器/视图结构?
我是否为位置和矢量创建控制器? 我应该创建一个设置控制器,并创建位置和矢量模型吗?
我真正的新Laravel,所以我有这样的疑问:Laravel 4设置控制器
如果我在数据库中有2个表,说:立场和载体,我希望能够 编辑/添加/更新/删除它们,我需要什么样的模型/控制器/视图结构?
我是否为位置和矢量创建控制器? 我应该创建一个设置控制器,并创建位置和矢量模型吗?
这完全取决于你,但是我会为每一个模型,一个所有逻辑的存储库,可能被称为SettingsRepository.php
,然后在需要使用该代码的任何位置使用该存储库。您还必须修改您的composer.json
文件,并确保您要放置存储库的文件夹位于autoload
部分。
至于你的控制器,你可能只需要一个从仓库抓取数据并将其插入到视图中的数据。当我想到设置时,我想到了应用程序的其他区域以及存储库中所需的设置,所有这些信息都可以被其他控制器轻松访问。
模型处理与数据库的直接交互。创建一个“位置”模型和一个“矢量”模型。当你这样做,你会延长雄辩模型,你很快就会有一个知道如何添加,删除,更新,保存等 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
}