牛骨文教育服务平台(让学习变的简单)
博文笔记

Creating a Simple CRUD App With Yii2 用 Yii2 创建一个简单的 CRUD (增删改查)应用

创建时间:2014-01-11 投稿人: 浏览次数:17345


原文来自http://www.yiiframework.com/wiki/490/creating-a-simple-crud-app-with-yii2

因为翻译能力有限,所以采用双语对照的形式,有译得不好的地方请多原谅。


Creating a SimpleCRUD App With Yii2 (Revised 12/20/2013)

用 Yii2创建一个简单的 CRUD(增删改查)应用(修订日期 2013-12-20)

Resources资源

WhatWe"ll Be Covering 我们将做些什么

WhatYou"ll Need 我们需要些什么

Disclaimers要求

CreatingYour App 创建你的应用

Creatinga Config file 创建一个配置文件

Creatingthe Database 创建一个数据库模型

Migrations迁移

Creatingthe Database Model 创建一个数据库模型

Creatingthe Controller 创建一个控制器

Views视图

HandlingAuthentication处理身份验证

ConcludingThoughts结束时的感想

Resources资源

Creating a Simple CRUDApp with Yii Framework 2 (Updated December 20th, 2013)

用 Yii创建一个简单的 CRUD(增删改查)应用(2013年12月20日修订)

By popular demand,today I will be revisitingmyprevious Yii Framework 2 Getting Started Guide and updating it with what iscurrently available in the Yii Framework 2 Repository. Provide here is an supersimple example CRUD application that will help you get started using theframework.

应大家的要求,今天我将要修订我前面写的一篇文章《Yii2框架入门指南》,我将用Yii2框架库里提供的最新内容来升级它。这里提供的是一个超简单的CRUD(增删改查)程序,将会帮助你从零起步使用这个框架。

Resources资源¶

Demo演示

OriginalBlog Entry 最早的博客实体

What We"ll Be Covering我们将做些什么¶

In this tutorial wewill be covering the following:

在这个教程中,我们将完成下面这些内容:

  • Creating a Basic App

  • 创建一基本的应用

  • Connecting to a MySQL Database

  • 连接到一个MySQL数据库

  • Creating Logic to handle CRUD of a single model

  • 创建逻辑来管理一个简单模型中的CRUD(增删改查)

  • Handling Basic Authentication

  • 处理身份验证

    What You"ll Need我们需要些什么¶

  • A Webserver setup with either Apache or Nginx.

  • 使用Apache或者 Nginx设置一个Web服务器,

  • A MySQL Database (5.5+)

  • 一个MySQL数据库(版本号在5.5以上)

  • Yii 2 Requirements (I"m running 5.5.x)

  • Yii2需求(我正使用的是PHP5.5.x)

    Disclaimers要求¶

    Before we getstarted...在我们开始之前...

  • Yii Framework 2 is by no means "Production Ready". Don"t use itin production. What is provided here is mainly used as an example of what youcan do in Yii Framework 2.

  • Yii2框架绝对没有做好“生产准备”,不要把它用于生产我在这里提供的,只能主要用作一个你学习使用Yii2框架的例子。

  • All the code released with this blog post is released under the MITLicense. Feel free to fork it and play with it.

  • 这个博客提供的所有代码,遵行 MIT许可发布。您可以自由地使用它和用它来建立分支。

  • The code provided is by no means "Production Ready". If you runthis code in a production environment you run it at your own risk.

  • 这里提供的代码绝对没有做好“生产准备”,如果你在一个生产环境中使用这些代码,你要对你自己的行为承担风险。

  • For simplicity, all commands will be from a Linux shell, and I will not beproviding specific Windows commands. Adapt them as necessary for Windows.

  • 为简单起见,所有的命令都是从 Linux终端上得来,我不会提供特定的针对Windows的命令。您可以修改它们,使之适用于Windows。

  • This article may be reproduced and distributed under the followingconditions -please don"t steal my writing and claim it as your own:

  • 这篇文章可以在一定条件下复制和发布——请不要窃取我的作品并将它据为已意。

    1. A link to the original articleMUST be provided in the reproduction:

      1.您在复制它时,必须要提供一个指向原文的链接。

      Erianna伊瑞安娜

My name and CopyrightMUST be provided in the reproduction:

2.在复制品中,还必须提供我的名字和版权。

Copyright&copy 2013 Charles R. Portwood II |Erianna

©版权所有2013,Charles R(查理斯·R)Portwood II (波德伍德二世)|Erianna伊瑞安娜

Creating Your App创建你的应用¶

When I wrote my firsttutorial covering this topic Yii didn"t have a basic web application yet. As ofthe time of writing, Yii now includes three web applications for you to testand try out. As with my last tutorial, we"re going to do everything fromscratch for the learning experience.

在我写我的第一个包含这个话题的教程时,Yii还没有一个基本的互联网应用。就在我的写作过程中,Yii已经包含了3个互联网应用来让你评测并试用它。作为我的最后一个教程,我们将为抓住学习经验做任何事情。

Creating Your WebSkeleton创建你的网络骨架

First, create a newfolder in your web directory. Inside that folder add the following folders:

首先,在你的Web文件夹下建立一个新的文件夹。在新建的文件夹中添加下列文件夹:

/config/

/web/

   /assets/

   /css/

/assets/

/commands/

/controllers/

/migrations/

/models/

/runtime/

/views/

   /site/

   /layouts/

A couple things tonotice here that are kinda important:

这里有一些比较重要的事情需要引起注意:

First, as of the timeof writing, the concept of a "protected" folder is gone. Yii lookalot more like the other PHP frameworks out there now (Codeigniter, Zend,CakePHP to name a few) in terms of base folder structure. The second thing tonotice is that we have 2 assets folders. The assets folder in the root of ourdirectory is going to contain stuff to handling assets, whereas the assetsfolder inside of our web/ directory is going tocontain our compiled assets.

首先,截至到正在写作的当下,“protected”文件夹的概念再没有。就基本目录结构而言,Yii现在看起来更像其他 PHP框架了(举几个来说,Codeigniter,Zend, CakePHP)第二个要关注的事情是,我们有两个assets文件夹我们根目录下的assets文件夹将要包含操控assets的文件,然而我们的web目录中的assets文件夹将包含我们编译的assets。

Now onto some files:

现在来到一些文件上:

The first file we wantto create isyii, which is equivalenttoyiic in Yii Framework 1.x.Create this file in the root of your directory, then add the following:

我们要创建的第一个文件是yii,它与Yii框架 1.x版本中的yiic文件相等。在我们的根目录下创建这个文件,然后写入下列内容:

#!/usr/bin/env php

<?php

defined("YII_DEBUG") ordefine("YII_DEBUG", true);

 

// fcgi doesn"t have STDIN definedby default

// fcgi默认没有定义 STDIN(标准输入)

defined("STDIN") or define("STDIN",fopen("php://stdin", "r"));

 

require(__DIR__ ."/vendor/autoload.php");

require(__DIR__ ."/vendor/yiisoft/yii2/yii/Yii.php");

 

$config = require(__DIR__ ."/config/console.php");

 

$application = newyiiconsoleApplication($config);

$exitCode = $application->run();

exit($exitCode);

Then, create a file calledAppAsset.php inassets/:

然后,创建一个文件,命名为AppAssetphp in assets/:

<?php

namespace appassets;

use yiiwebAssetBundle;

 

class AppAsset extends AssetBundle

{

   public $basePath = "@webroot";

   public $baseUrl = "@web";

   public $depends = [

       "yiiwebYiiAsset",

       "yiiootstrapBootstrapAsset",

   ];

}

Finaly, create yourindex.php bootstrap file inweb/index.php:

最后,在web文件夹中创建你的index.php和bootstrap文件:

<?php

 

// comment out the following twolines when deployed to production

//在部署到生产环境时,请注释掉下面的这两行

defined("YII_DEBUG") ordefine("YII_DEBUG", true);

 

require(__DIR__ ."/../vendor/autoload.php");

require(__DIR__ ."/../vendor/yiisoft/yii2/yii/Yii.php");

 

$config = require(__DIR__ ."/../config/web.php");

 

(newyiiwebApplication($config))->run();

This last part is veryimportant. Your web directory needs to point toweb/index.php rather thanindex.php. Until you make this change youwon"t be able to see anything when you run your application. We"re not at thepoint where you can see stuff yet, but it"s important to point this out firstso that we can make sure it"s taken care of.

这最后的部分是非常重要的。你的站点文件夹要指向web/index.php,而不是index.php。直到你完成这些改变,你运行自己的程序才能够看到一些东西。我们不是把你能看到这些东西当作要点,不过是因为先指出这一点很重要,以便我们能确信它能得到好的关注。

With these two filesin place we can create our composer file that will download and install YiiFramework 2 for us. Create a new file in the root of our appliation calledcomposer.json

这两个文件准备就绪之后,我们就能创建我们的composer(设计)文件,它能为我们下载并且安装Yii2框架。在我们的程序根目录下创建一个文件,命名为composer.json。

{

       "name": "A Basic CRUDApp",

       "description": "Yii 2Basic CRUD Example",

       "keywords": ["yii","framework", "basic", "CRUD"],

       "homepage":"http://yf2.erianna.com",

       "type": "project",

       "license": "MITC",

       "minimum-stability":"dev",

       "require": {

               "php":">=5.4.0",

               "yiisoft/yii2":"*",

               "yiisoft/yii2-bootstrap": "*",

               "yiisoft/yii2-codeception": "*",

               "yiisoft/yii2-debug":"*",

               "yiisoft/yii2-gii":"*",

               "yiisoft/yii2-swiftmailer": "*"

       }, 

       "scripts": {

               "post-create-project-cmd": [

                       "yiicomposerInstaller::setPermission"

               ]  

       }, 

       "extra": {

               "writable": [

                       "runtime",

                       "web/assets"

               ], 

               "executable": [

                       "yii"

               ]  

       }  

}

Next we"ll be usingcomposer to install Yii for us. If you already have composer installed you canskip ahead.

下一步,我们要用composer来为我们安装Yii。如果你已经有了composer,你可以路过前面的内容。

Install Composer安装Composer

Yii 2 now usescomposer to download additional packages Yii uses. Before we can get startedusing Yii we"ll need to download and install composer.

Yii2现在使用composer来下载它它所使用的安装包。在我们能够开始使用Yii之前,我们得下载并安装composer。

curl -sShttps://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin

sudo ln -s /usr/bin/composer.phar/usr/bin/composer

Then install thecomposer dependencies

然后安装composer的依赖文件。

sudo composer self-update

composer install

Installing Yii安装Yii

With the installationof composer taken care of, we can now install Yii and it"s dependencies

在细心安装了composer之后,我们可以安装Yii和它的依赖文件。

# Install Yii Dependencies

composer install composer安装

If everything goeswell, you should see a bunch of output that looks similar to the following. Ifyou get an error, fix it and try runningcomposer install again.

如果一切顺利,你可能看到一个输出的分支,它看起来与下面的内容相似。如果你遇上了一个错误,解决它并且重新安装composer。

Loading composer repositories with package information

加载包含有包信息的composer库。

Installing dependencies (includingrequire-dev)

 - Installing ezyang/htmlpurifier (v4.5.0)

   Loading from cache

 

 - Installing michelf/php-markdown (1.3)

   Loading from cache

 

 - Installing phpspec/php-diff (dev-master 30e103d)

   Cloning 30e103d19519fe678ae64a60d77884ef3d71b28a

 

 - Installing yiisoft/jquery (1.10.2)

   Loading from cache

 

 - Installing yiisoft/yii2-composer (dev-master 96ecb97)

   Cloning 96ecb97c2083231706147f026d104d82a7202ad0

 

 - Installing swiftmailer/swiftmailer (dev-master f0be830)

   Cloning f0be8302f28913af7bd7df6639e5bec5b5e79257

 

 - Installing twbs/bootstrap (dev-master 2854c5c)

   Cloning 2854c5c3fb65b709fbf32d05faccf7a294626cca

 

 - Installing yiisoft/yii2 (dev-master e6ac68b)

   Cloning e6ac68b89c520befbcb4682880ac8284f1d094dd

 

 - Installing yiisoft/yii2-codeception (dev-master d7e6e58)

   Cloning d7e6e5806483647a5fee6462bb216e67773d9e88

 

 - Installing yiisoft/yii2-bootstrap (dev-master 54fedb4)

   Cloning 54fedb4c22b057b27ff088d11e6f411992956eeb

 

 - Installing yiisoft/yii2-debug (dev-master 97e2460)

   Cloning 97e24600932d511f419c114ef1d44e85211c47c2

 

 - Installing yiisoft/yii2-gii (dev-master 84bb194)

   Cloning 84bb19424561b744167636fb893701a15368d58b

 

 - Installing yiisoft/yii2-swiftmailer (dev-master d378f7d)

   Cloning d378f7d6d731c8130597411935d7ee05aa73897a

 

Writing lock file

写锁定文件

Generating autoload files

生成自动加载文件

Creating a Config file创建一个配置文件¶

Now that we"ve setupour application to be bootstrapped, we can create a config file. The configfiles for Yii 2 are p

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。