jworkman / scaffold-bundle
Symfony Scaffold Bundle
Requires
- php: >=5.4.0
- symfony/symfony: >=2.4
- dev-master / 1.0.x-dev
- v1.0.2
- v1.0.1
- dev-bugs/column-mask-pk
- dev-features/banana-messages
- dev-features/controller-hooks-form
- dev-bugs/controller-hooks-redeclare
- dev-features/controller-hooks
- dev-bugs/form-type-decimal
- dev-improvements/raw-values
- dev-features/generator
- dev-bugs/generator-routes
- dev-features/view-override
- dev-features/global-twig-layout
- dev-features/form-generator
- dev-features/documentation
- dev-features/api
This package is auto-updated.
Last update: 2024-08-29 04:46:21 UTC
README
ScaffoldBundle 允许你在 Symfony 应用程序中快速构建 CRUD 功能。它还允许你为每个支架提供 JSON 响应。结合 Ruby on Rails 和 Symfony 的最佳部分。使用 ScaffoldBundle,你可以围绕 Doctrine 实体生成灵活的 CRUD 功能。
要求
php 版本 >= 5.4 Symfony 版本 >= 2.7
安装
使用 composer,你可以在 Symfony 项目的根目录下运行以下命令来安装该包
composer require jworkman/scaffold-bundle
你必须将包添加到你的 app/AppKernel.php
。
$bundles = array(
...
new JWorkman\ScaffoldBundle\JWorkmanScaffoldBundle(),
);
就这样!包应该已经安装好了。现在你可以开始创建自己的支架了。
入门
为了生成支架,你必须在你项目中定义一些 Doctrine 实体。假设我们在 AcmeBundle
包中定义了一个名为 "UserProfile" 的实体。我们必须将该实体引用为 AcmeBundle:UserProfile
。我们可以在该模型上运行支架生成命令以生成支架控制器。
app/console scaffold:generate
第一个问题是要求指定要为其实例化支架的实体。在我们的例子中,它将是 AcmeBundle:UserProfile
。
The Entity shortcut name: AcmeBundle:UserProfile
第二个问题是询问你希望为你的支架使用什么友好的名称。这应该是一个用户可读的标题,将在所有 CRUD 视图中显示为支架实体的通用名称。
注意:此值应该是单数(不是复数)。
Specify a friendly common title to use [User Profile]:
第三个问题将询问你希望为该支架使用什么路由前缀。在我们的例子中,如果我们正在编辑一个位于 /user_profile/4/edit
的 UserProfile,则 user_profile
是我们的前缀。它是支架的唯一范围/命名空间。
Specify a routing prefix to mount this scaffold [user_profile]:
下一个问题将询问你是否想要锁定此支架的任何 CRUD 功能。阅读问题上方的文档以获取更多信息。你必须列出所有要锁定的方法,并用逗号分隔。
What HTTP do you want to disable (comma separated)? none
// Or if you want to disable the index, and edit actions
What HTTP do you want to disable (comma separated)? index,edit
第五个问题将询问你是否想要在索引操作中隐藏任何特定的字段。这不会隐藏字段从编辑或新操作。这对于隐藏像密码这样的字段非常有用,这些字段不应从索引操作中隐藏,但不应从编辑或新表单中隐藏。以下我们将禁用字段 "password"。
Specify any field you would like to make private (Enter nothing to stop adding fields): password
接下来的几个问题将遍历你的实体上的每个字段,并询问你为该字段使用什么表单字段类型。大多数时候,生成器将根据你的实体字段类型生成智能默认值。这可以在以后进行调整。
一旦你定义了你的表单字段类型,它将询问你是否想为这个特定的支架启用 JSON API 功能。如果不使用它,则保持禁用是一种好习惯。在我们的例子中,我们将启用它。
Would you like to enable the JSON API for this scaffold? Yes
我们需要定义一个目标包。这默认为实体所在的包,但可以覆盖为不同的包。如果你有一个特定的包,你想要将所有的支架放置在那里而不是单独的包,这很有用。
Specify a target bundle for the controller [AcmeBundle]:
接下来是更新路由。为了让脚手架控制器正确地将请求映射到它,需要将一些路由添加到目标包的 routing.yml
文件中。它会询问您是否希望它自动为您完成。完成此操作后,您将在文件末尾注意到一些新路由。
Would you like to automatically update your routes as well? Y
生成器完成后,您会注意到它在目标包中放置了一个新的控制器。在我们的例子中,它是 src/AcmeBundle/Controller/UserProfileController.php
。如果您打开它,它看起来可能如下所示
<?php
namespace AcmeBundle\Controller;
use JWorkman\ScaffoldBundle\Controller\ScaffoldController;
class UserProfileController extends ScaffoldController
{
/*
entityName is the fully qualified entity name including the bundle
namespace separated by a colon. An entity name must be specified
in order to place a scaffold in context.
Ex) To make the scaffold manage the "Event" entity
protected $entityName = "AcmeBundle:Event";
*/
protected $entityName = "AcmeBundle:UserProfile";
/*
viewParameter is a property that you can specify twig to use for any
entity results that are passed to the view.
Ex) To use the twig name "events" in twig
protected $viewParameter = "events";
*/
protected $viewParameter = "results";
/*
friendlyName is a property that you can specify the official title
of this scaffold. This should be user friendly and formated to fit
most method contexts.
Ex) To make the scaffold refer to your "UserProfile" entity as "User Profiles"
protected $friendlyName = "User Profile";
*/
protected $friendlyName = "User Profile";
/*
lockedMethods allows you to specify a list of forbidden methods on a
scaffold. This defaults to all methods allowed.
Ex) If you want to disabled the ability to delete an entity
protected $lockedMethods = ['index','delete'];
*/
protected $lockedMethods = [];
/*
columnMask allows you to specify a list of entity columns to pass to the
view. If you are dealing with passwords this should be setup to hide the
password.
Ex) To disable the "password" column of an entity
protected $columnMask = [ 'password' => false ];
*/
protected $columnMask = [];
/*
limit controls the maximum amount of results that can be loaded on one
request. This is used in conjunction with pagination.
*/
protected $limit = 40;
/*
apiEnabled specifies if this entity should use JSON API support. To turn
the API on just set this to true. The API will show all the columns that
are specified by the columnMask property. This defaults to disabled.
Ex) To enable the API of an entity
protected $apiEnabled = true;
*/
protected $apiEnabled = false;
/*
routes specify the different route names for each action as they
relate to your routing.yml file. This is important so all of the
scaffold links are correct. It is formated by 'method' => 'route_name'
Ex)
protected $routes = [ 'index' => 'event_index_route_name' ]
*/
protected $routes = [
'index' => 'user_profile_index',
'new' => 'user_profile_new',
'edit' => 'user_profile_edit',
'create' => 'user_profile_new',
'delete' => 'user_profile_delete',
'update' => 'user_profile_edit'
];
/*
This method should return a symfony form for the $entity param. An
example is:
return $this->createFormBuilder( $entity )
->add('title', 'text')
*/
public function getForm( $entity )
{
return $this->createFormBuilder( $entity )
->add("username", "text")
->add("password", "hidden")
->add("save", "submit")
;
}
}
您会发现我们可以更改表单及其字段类型。还有其他一些可以在任何时候覆盖的属性,以进一步自定义脚手架。您还可以在此处添加一些属性。例如,如果我们想覆盖默认的脚手架twig模板,我们可以在控制器中定义一个名为 templates
的属性,该属性指向一个包视图资源目录。
以下示例显示了如何覆盖CRUD模板,并使用另一个包的模板。在脚手架控制器中添加以下属性
/*
templates can override the default views for a scaffold. If you have your
own view templates you would like to use then use this property to point
the scaffold towards your custom templates. It must be pointed at the
directory that contains the following directory structure:
Ex)
protected $templates = "AcmeBundle:Events";
would point to the following directory structure:
AcmeBundle
- Resources
-- views
--- Events
---- index.html.twig
---- edit.html.twig
---- new.html.twig
*/
protected $templates = "AcmeBundle:UserProfile";
上面的示例将使用 @AcmeBundle/Resources/views/UserProfile
内部的视图。
布局模板
为了让脚手架正常工作,您必须定义一个基本布局文件供其使用。默认的基本布局文件应放在 app/Resources/views/scaffold.html.twig
中。您可以在该文件中定义自己的布局,或使用以下命令生成一个布局
app/console scaffold:layout
生成器将为脚手架创建一个新的布局文件。现在您必须添加您需要的任何样式。