crud自动生成_使用Phreeze自动生成CRUD应用

crud自动生成

Back in the times of Symfony 1.x, the framework had a powerful backend module to help the developers and site administrators create a good enough app and provide necessary CRUD features to manage the database (and save us from using PHPMyAdmin).

早在Symfony 1.x时代,该框架就有一个功能强大的后端模块,可帮助开发人员和站点管理员创建足够好的应用程序,并提供必要的CRUD功能来管理数据库(并避免使用PHPMyAdmin)。

Since Symfony 2, this has been taken out from the core and the developers either have to rely on their own to start from scratch or rely on some other third party Symfony 2 bundle when such a feature is needed – and in many circumstances, it is.

从Symfony 2开始,此功能已从核心中删除,开发人员要么需要依靠自己的资源从头开始,要么在需要此功能时依赖于其他第三方Symfony 2捆绑软件–在许多情况下,它是。

In this article, we will take a look at Phreeze, a simple and easy to use PHP framework that can help us generate a MySQL CRUD backend app. I will use my book collection test site as the underlying database. Please refer to my Data Fixtures in Symfony2 article for more details on the database structure.

在本文中,我们将介绍Phreeze ,这是一个简单易用PHP框架,可以帮助我们生成MySQL CRUD后端应用程序。 我将使用我的图书藏书测试站点作为基础数据库。 请参阅我在Symfony2中的“数据装置”一文,以获取有关数据库结构的更多详细信息。

安装和引导 (Installation and bootstrapping)

Phreeze is distributed as standalone. You can clone a copy of its latest version from Github.

Phreeze作为独立发行。 您可以从Github克隆其最新版本的副本。

In my environment, I have created a new virtual host test and cloned the repository to its phreeze folder so that I can start the backend generation with Phreeze using: http://test/phreeze/builder.

在我的环境中,我创建了一个新的虚拟主机test ,并将存储phreeze到其phreeze文件夹中,以便可以使用以下http://test/phreeze/builder开始使用Phreeze进行后端生成: http://test/phreeze/builder

To facilitate the generation of a backend, Phreeze introduces two step wizard-like screens to guide us.

为了促进后端的生成,Phreeze引入了两步式向导式屏幕来指导我们。

In the first screen, we will provide the necessary database connection information (server, database/schema, user, password):

在第一个屏幕中,我们将提供必要的数据库连接信息(服务器,数据库/模式,用户,密码):

alt

On MySQL Driver options, Phreeze provides raw mysql_connect, mysqli_connect and also PDO. PDO is recommended as it is more popular nowadays and mysql_* is deprecated.

在MySQL驱动程序选项上,Phreeze提供了原始的mysql_connectmysqli_connect以及PDO。 建议使用PDO,因为它在当今更为流行,并且不建议使用mysql_*

Next, we can click “Analyze Database” and it will bring us to the second and final screen:

接下来,我们可以单击“分析数据库”,它将带我们进入第二个也是最后一个屏幕:

alt

All the tables in the database are identified and selected by default. If we don’t want to include a particular table in our CRUD backend, we shall deselect it. In my case, I have deselected the table book_visit as this table stores the visit information of book detail pages and is populated inside my app.

默认情况下,将标识并选择数据库中的所有表。 如果我们不想在CRUD后端中包含特定的表,则应取消选择该表。 就我而言,我已取消选择book_visit表,因为该表存储了书详细信息页面的访问信息,并已填充到我的应用程序中。

Phreeze also suggests the names of the generated entities for each table. The name in the single form (BookBook) reflects one entity (a book) of a table (book_book), while that in the plural form (BookBooks) reflects the collection class (all books) of that table (book_book).

Phreeze还建议为每个表生成的实体的名称。 单一形式( BookBook )的名称反映表( book_book )的一个实体(一本书),而复数形式( BookBooks )的名称反映该表( book_book )的集合类(所有书籍)。

Phreeze is quite smart when suggesting the plural forms (as can been seen in Lakers/Lakerses). So normally I will just leave the suggestions untouched.

当建议复数形式时,Preeze非常聪明(如在Lakers / Lakers Lakerses )。 因此,通常我将不动摇建议。

We can safely leave “Column Prefix” blank. It is used to strip (NOT append) that column prefix from your table’s field names during the entities’ generation for the backend. For example, if your table has a field called fld_firstname, you can provide a Column Prefix as fld_ and thus in the generated classes, that field will be mapped to a member named as firstname instead of fld_firstname.

我们可以放心地将“列前缀”留空。 它用于在为后端实体生成期间从表的字段名称中删除(不附加)该列前缀。 例如,如果您的表有一个名为fld_firstname的字段,则可以提供一个列前缀为fld_ ,因此在生成的类中,该字段将映射到一个名为firstname的成员,而不是fld_firstname

Next, in the “Application Options”, Phreeze offers a few dropdowns to let us tweak the generated backend app.

接下来,在“应用程序选项”中,Phreeze提供了一些下拉菜单,让我们调整生成的后端应用程序。

Packages To Generate

要生成的包

It offers two types of packages: one is the running app with different template engines (Savant, Laraval/Blade, Smarty). The Twig template is not supported yet so in my Symfony 2 site, I have to use Smarty for my backend views. All templates will use the Bootstrap CSS boiler plate. This will help us to further customize the layout, style and so on of the backend view in a later stage.

它提供两种类型的软件包:一种是具有不同模板引擎(Savant,Laraval / Blade,Smarty)的正在运行的应用程序。 尚不支持Twig模板,因此在我的Symfony 2网站上,我必须使用Smarty作为后端视图。 所有模板都将使用Bootstrap CSS样板。 这将有助于我们在以后的阶段进一步自定义后端视图的布局,样式等。

Another type of package is called the test package. If supports PHPUnit and QUnit. We will not cover this in this article.

另一种类型的程序包称为测试程序包。 如果支持PHPUnit和QUnit。 我们将不在本文中介绍。

Application Name and Root URL

应用程序名称和根URL

For application name, just give it a meaningful one of your choice.

对于应用程序名称,只需给它一个有意义的选择即可。

We need to be careful when setting the application root url. For now, we just put a name for it and let’s come back later to this setting.

设置应用程序根URL时,我们需要小心。 现在,我们只是为其命名,稍后再回到此设置。

Path to lib and Self-Contained

通往自由与自我的道路

Leave the “Path to libs” as it is.

保持“ libs路径”不变。

I will choose “Yes” to make the generated backend application self-contained.

我将选择“是”以使生成的后端应用程序独立。

Long Polling

长轮询

Choose “Disabled”, which is also the default option. Long polling makes sure the interface is refreshed via ajax, which allows you to use the app in several tabs and instantly see changes made in one happen in others, without having to reload them. This is eye candy for the most part, and is not essential.

选择“禁用”,这也是默认选项。 长时间轮询可确保通过ajax刷新界面,使您可以在多个选项卡中使用该应用程序,并立即查看在其他选项卡中所做的更改,而无需重新加载它们。 这在大多数情况下都是眼药,并非必需。

Now, let’s click “Generate Application”. Phreeze will start to generate all the necessary files and zip them into one archive (in this case, this file is named “backend.zip“).

现在,让我们单击“生成应用程序”。 Phreeze将开始生成所有必需的文件,并将它们压缩到一个存档中(在这种情况下,此文件名为“ backend.zip ”)。

The generated zip file is quite big (almost 1M in my case) but most of it is contributed by Bootstrap files (CSS, image, js, font, etc) and the PHAR archive for Phreeze (we selected “Self-Contained” and included the Phreeze framework for our app).

生成的zip文件非常大(以我为例,几乎为1M),但其中的大部分是由Bootstrap文件(CSS,图像,js,字体等)和Phreeze的PHAR存档(我们选择“自包含”并包含适用于我们应用的Phreeze框架)。

放在Symfony 2项目下 (Put it under the Symfony 2 project)

The backend.zip file created above is everything we need to launch the backend. To integrate those unzipped files into Symfony 2, my approach will be as follows:

上面创建的backend.zip文件是启动后端所需的一切。 要将这些未压缩的文件集成到Symfony 2中,我的方法如下:

  • Recall what we have provided for “Application Root URL”. In this case, it is http://servername/backend.

    回顾我们为“应用程序根URL”提供的内容。 在这种情况下,它是http://servername/backend

  • My site that will use this backend has server name rsywx so the URL to call my backend should be something like http://rsywx/backend.

    我将使用此后端的站点的服务器名称为rsywx因此调用我的后端的URL应该类似于http://rsywx/backend

  • As Symfony 2 puts everything “static” into its web directory, I created the backend directory under that web directory and unzipped all files in backend.zip into that directory.

    当Symfony 2将所有“静态”内容放入其web目录时,我在该web目录下创建了一个backend目录,并将backend.zip所有文件解压缩到该目录中。

To launch the backend, we just visit http://rsywx/backend and the backend welcoming screen will be like this:

要启动后端,我们只需访问http://rsywx/backend ,后端欢迎屏幕将如下所示:

alt

(NOTE: Due to the Symfony 2 .htaccess rewrite rules and also your LAMP/WAMP configuration, you may not see anything or just see a directory listing. In that case, try visiting http://rsywx/backend/index.php. The latter URL will always work.)

( 注意:由于Symfony 2 .htaccess重写规则以及LAMP / WAMP配置,您可能看不到任何内容或仅看到目录列表。在这种情况下,请尝试访问http://rsywx/backend/index.php 。后一个网址将始终有效。)

At the top of the page, we see a few navigation links. Each will bring us to the respective table’s CRUD page. Let’s take a look at a few of them and try to CRUD a few records.

在页面顶部,我们看到一些导航链接。 每个都将带我们到相应表的CRUD页面。 让我们看看其中的一些,并尝试对一些记录进行CRUD。

(NOTE: CRUD operations can drastically change your database/tables, so for this demonstration, I will only apply these operations on a test database. The test database can always be dumped with the same set of data using DataFixtures discussed in my previous article.)

( 注意: CRUD操作可以极大地更改您的数据库/表,因此,在本演示中,我仅将这些操作应用于测试数据库。使用我在上一篇文章中讨论的DataFixtures可以始终将测试数据库与同一组数据一起转储。 )

The following few screenshots show what will be displayed for book_publishers, book_places, and book_taglist (doing an insert operation):

以下几个屏幕快照显示了book_publishersbook_placesbook_taglist显示book_taglist (执行插入操作):

alt
alt

Notice that as book_taglist has an FK linked to book_book, the bid field is in a dropdown box so that we can select a valid book ID to avoid consistency check failures.

请注意,由于book_taglist具有链接到book_book的FK,因此bid字段位于下拉框中,因此我们可以选择有效的Book ID以避免一致性检查失败。

Also, in each page shown above, there is a “Search” function with which we can further filter the results displayed.

同样,在上面显示的每个页面中,都有一个“搜索”功能,我们可以使用该功能进一步过滤显示的结果。

Clicking on table’s head will sort the result in that particular field, switching from ascending and descending. Quite handy, right?

单击表格的头部将在该特定字段中对结果进行排序,从升序和降序切换。 很方便,对不对?

Now let’s take a look at the display of the book_book table. We click the “BookBooks” link in the top navigation bar and expect to see a total of 101 books. Oops! Why is there only one book displayed?

现在,让我们看一下book_book表的显示。 我们单击顶部导航栏中的“ BookBooks”链接,并希望看到总共101本书。 糟糕! 为什么只显示一本书?

After some discussion with the author of Phreeze, we managed to figure out the issue: in my book_book table setup, there is a field named page. Phreeze’s internal route/controllers also use a page parameter to paginate the result set (like page=1 to display the first page). But as page is also a valid field name of my table, Phreeze thinks that I am trying to filter the result set to display only those records whose page field has a value of 1.

与Phreeze的作者进行了讨论之后,我们设法找出了问题所在:在book_book表设置中,有一个名为page的字段。 Phreeze的内部路由/控制器还使用page参数对结果集进行分页(例如page=1以显示第一页)。 但是由于page也是表的有效字段名称,因此Phreeze认为我试图过滤结果集以仅显示其page字段值为1的那些记录。

Well, in my test data, there is exactly only one record whose page is of value 1. That explains why there is only one record displayed.

好吧,在我的测试数据中,只有一条记录的page的值为1。这解释了为什么只显示一条记录的原因。

The author (thanks, Jason!) provides two solutions to get out of this.

作者(感谢Jason!)提供了两种解决方案。

One option is to rename that field to something else, say pages. But this will involve a lot in our Symfony 2 setup, including re-import the database mappings and re-generate the entities. So I just denied it.

一种选择是将该字段重命名为其他名称,例如pages 。 但这将在我们的Symfony 2设置中涉及很多内容,包括重新导入数据库映射并重新生成实体。 所以我只是否认了。

The second, which is what I use, is to hack a bit on the generated code. Here is how:

我使用的第二个方法是对生成的代码进行一些修改。 方法如下:

  • Locate and open the BookBookController.php file. It should reside in our Symfony 2 project under web/backend/libs/Controller if you follow the setup instructions above.

    找到并打开BookBookController.php文件。 如果您按照上面的设置说明进行操作,它应该位于我们的Symfony 2项目中的web/backend/libs/Controller

  • Somewhere around line 60-70, inside the function called Query(), there should be these few lines:

    在第60-70行附近的某个位置,在名为Query()的函数内,应该有以下几行:

if (property_exists($criteria, $prop_normal))
{
    $criteria->$prop_normal = RequestUtil::Get($prop);
}
elseif (property_exists($criteria, $prop_equals))
{
    // this is a convenience so that the _Equals suffix is not needed
    $criteria->$prop_equals = RequestUtil::Get($prop);
}
  • In our particular case where our table field page conflicts with Phreeze query parameter page, we modify the above code to grant one exception:

    在我们的特殊情况下我们的表中的字段page与Phreeze查询参数冲突page ,我们修改上面的代码授予一个例外:

if ($prop != 'page' && property_exists($criteria, $prop_normal))
{
    $criteria->$prop_normal = RequestUtil::Get($prop);
}
elseif ($prop != 'page' && property_exists($criteria, $prop_equals))
{
    // this is a convenience so that the _Equals suffix is not needed
    $criteria->$prop_equals = RequestUtil::Get($prop);
}
  • Save and reload.

    保存并重新加载。

Bingo! Here is what is displayed for our book CRUD page:

答对了! 这是我们的图书CRUD页面显示的内容:

alt

The book list is displayed correctly and the pagination is working fine. (The above screenshot shows we are in page 4.)

图书清单显示正确,并且分页工作正常。 (以上屏幕截图显示了我们在第4页中。)

更改应用程序配置 (Changing the app configurations)

Phreeze relies on three configuration files to run correctly.

Phreeze依赖于三个配置文件才能正确运行。

  • _machine_config.php: This file holds the the database connection parameters and also the Application Root URL settings. We need to change the settings when we deploy.

    _machine_config.php :该文件包含数据库连接参数以及应用程序根URL设置。 部署时需要更改设置。

  • _global_config.php: This file defines many application specific settings and fundamental functions. Normally, we will leave it as it is.

    _global_config.php :此文件定义许多特定于应用程序的设置和基本功能。 通常,我们将保持原样。

  • _app_config.php: This file defines all the routes and other settings. Normally, it should not be modified.

    _app_config.php :此文件定义所有路由和其他设置。 通常,不应对其进行修改。

蜜蜂 (APIs)

The most brilliant feature of Phreeze is that it provides an API interface. To call the API directly and see its JSON output, we add api in the URI we call to display the data.

Phreeze最杰出的功能是它提供了API接口。 要直接调用API并查看其JSON输出,我们在要显示数据的URI中添加api

For example, to display the book data, the URI is: http://rsywx/backend/bookbooks; to get the JSON output from API, we use this: http://rsywx/backend/api/bookbooks.

例如,要显示书籍数据,URI为: http://rsywx/backend/bookbooks ; 要从API获取JSON输出,我们使用以下代码: http://rsywx/backend/api/bookbooks

This API interface has two shining points:

该API接口有两个亮点:

  1. When we customize the views, we do have a way to get the data directly from the respective APIs. Then we can use JSON decode to parse the returned data and use them in our template engine. This will save us a lot of time and effort.

    自定义视图时,确实有一种方法可以直接从相应的API获取数据。 然后,我们可以使用JSON解码来解析返回的数据,并在我们的模板引擎中使用它们。 这将为我们节省大量时间和精力。
  2. It actually becomes an out-of-the-box RESTful API to provide data to 3rd parties.

    它实际上变成了现成的RESTful API,可以为第三方提供数据。

And of course, with the raw data grabbed by Phreeze, we may also be able to find some other not so obvious issues and bugs.

当然,利用Phreeze捕获的原始数据,我们也许还能找到其他一些不太明显的问题和错误。

保护访问 (Secure the access)

Right now, this backend URL (http://rsywx/backend/index.php) is accessible to everyone. In most cases this is not a good idea so let’s secure the access to this page, or more precisely, to this directory (web/backend).

现在,每个人都可以访问http://rsywx/backend/index.php URL( http://rsywx/backend/index.php )。 在大多数情况下,这不是一个好主意,因此,让我们保护对该页面的访问,或更准确地说,对此目录( web/backend )的访问。

Phreeze is not a bundle in Symfony 2’s terms. So Symfony’s built-in authorization does not apply and the access to /backend will let anyone through.

Phreeze不是Symfony 2的捆绑产品。 因此,Symfony的内置授权不适用,对/backend的访问将使任何人通过。

In this case, I am using the combination of .htaccess and .htpasswd to provide a quite entry-level security control on this directory using HTTP authentication.

在这种情况下,我将使用.htaccess.htpasswd的组合使用HTTP身份验证在此目录上提供完全入门级的安全控制。

NOTE: Phreeze comes with its own authentication too. It uses a table to store the user name and hashed password. But in this short entry tutorial, I am giving it a miss. Interested parties can download and read its authorization demo to get a better understanding.

注意: Phreeze也带有自己的身份验证。 它使用一个表来存储用户名和哈希密码。 但是在这篇简短的入门教程中,我很想念它。 有兴趣的人士可以下载并阅读其授权演示以获得更好的理解。

Phreeze has come with its own .htaccess file and we just append a few lines to its end:

Phreeze带有自己的.htaccess文件,我们在其末尾添加了几行:

AuthName "RSYWX Backend"
AuthType Basic
AuthUserFile .htpasswd
require valid-user

Then we can create a .htpasswd file with the htpasswd utility under Linux. We copy that .htpasswd file to a proper location. In my Windows EasyPHP installation, it should be copied to f:\EasyPHP\binaries\apache\. Don’t panic if you can’t figure out the right location at first. If this file is in the wrong location, there will be a 500 error and the Apache error.log will always prompt you with the right location.

然后,我们可以在Linux下使用htpasswd实用程序创建一个.htpasswd文件。 我们将该.htpasswd文件复制到正确的位置。 在Windows EasyPHP安装中,应将其复制到f:\EasyPHP\binaries\apache\ 。 如果一开始就找不到正确的位置,请不要惊慌。 如果此文件在错误的位置,将出现500错误,并且Apache error.log将始终以正确的位置提示您。

NOTE: In a Symfony 2 site, HTTP authentication not using Symfony’s built-in authorization method (security.yml) may fail. It works fine in my current local EasyPHP setup but fails in my production server setup and the difference is the Apache version. Apache 2.4 is recommended.

注意:在Symfony 2站点中,不使用Symfony的内置授权方法( security.yml )的HTTP身份验证可能会失败。 在我当前的本地EasyPHP设置中,它工作正常,但在生产服务器设置中,它却失败了,区别在于Apache版本。 建议使用Apache 2.4。

一些想法和结论 (A few thoughts and conclusion)

Phreeze comes in very handy for me. With its help, I managed to create my backend CRUD module for my site within 10 minutes without writing a single line of code (except the hacking stuff, of course).

微风对我非常有用。 在它的帮助下,我设法在10分钟内为我的站点创建了后端CRUD模块,而无需编写任何代码(当然,除了黑客之类的东西之外)。

Its features are not super rich but are sufficient. I like the API, filtering and sorting very much.

它的功能不是超级丰富,但是足够了。 我喜欢API,非常喜欢过滤和排序。

Its interface may still need to be enhanced. Currently, it relies purely on JavaScript and DOM manipulation to dynamically display new data (say, when we navigate to next page). The URL will remain the same but I would like to see something like http://rsywx/backend/bookbooks/page/2 so that I can quickly navigate to a page I want, instead of clicking the pagination links many times (it only has previous/next page link and total 10 page links).

其界面可能仍需要增强。 当前,它完全依靠JavaScript和DOM操作来动态显示新数据(例如,当我们导航到下一页时)。 该URL将保持不变,但我希望看到类似http://rsywx/backend/bookbooks/page/2这样我可以快速导航到所需的页面,而不必多次单击分页链接(仅具有上一页/下一页链接和总共10个页面链接)。

I would yearn for Twig template support. Symfony 2 users use Twig a lot and Twig itself is quite mature and popular. I hope to see this in its future releases.

我渴望获得Twig模板支持。 Symfony 2用户经常使用Twig,而Twig本身已经相当成熟和流行。 我希望在以后的版本中看到这一点。

Missing other DB support may restrict its application scope. In my personal view, when MySQL is supported via PDO, it is not a mission impossible to support other popular DBs (SQLite, pgSQL, MongoDB, etc). I hope this is in the author’s roadmap.

缺少其他数据库支持可能会限制其应用范围。 以我个人的观点,当通过PDO支持MySQL时,支持其他流行的DB(SQLite,pgSQL,MongoDB等)并非不可能。 我希望这是作者的路线图。

It may not have those fancy interfaces like other admin generator bundles in Symfony 2 but it is enough for a simple and quick backend setup and usage. And after all, we can use our own creativity to give it a brand new look.

它可能没有Symfony 2中其他管理生成器包那样的精美界面,但是对于简单,快速的后端设置和使用来说已经足够了。 毕竟,我们可以利用自己的创造力给它一个全新的外观。

This also has the advantage that it is not tightly bound to Symfony 2. You can use it in any of your MySQL powered sites with other frameworks.

这还有一个优势,就是它没有与Symfony 2紧密绑定。您可以在任何具有其他框架MySQL支持站点中使用它。

For a detailed documentation on Phreeze, please refer to its official site.

有关Phreeze的详细文档,请访问其官方网站

I would give Phreeze a 4 out of 5 score and recommend you use it in your own site.

我会给Phreeze 5分中的4分,建议您在自己的网站中使用它。

Let me know your feedback and comments!

让我知道您的反馈和意见!

翻译自: https://www.sitepoint.com/autogenerate-crud-apps-phreeze/

crud自动生成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值