>  기사  >  백엔드 개발  >  CakePHP 官方博客示范教程

CakePHP 官方博客示范教程

WBOY
WBOY원래의
2016-06-13 11:03:281100검색

CakePHP 官方博客示例教程

初识CakePHP,感觉不错;

按照官方的博客示例教程练习了一下,有点自信继续学下去了。下面是示例:

地址:http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

?

?

Blog Tutorial - Adding a layer

Create a Post Model

The Model class is the bread and butter of CakePHP applications. By creating a CakePHP model that will interact with our database, we’ll have the foundation in place needed to do our view, add, edit, and delete operations later.

CakePHP’s model class files go in?/app/Model, and the file we’ll be creating will be saved to?/app/Model/Post.php. The completed file should look like this:

<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">class</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nc">Post</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">extends</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nx">AppModel</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span></span>

Naming convention is very important in CakePHP. By naming our model Post, CakePHP can automatically infer that this model will be used in the PostsController, and will be tied to a database table called?posts.

Note

CakePHP will dynamically create a model object for you if it cannot find a corresponding file in /app/Model. This also means that if you accidentally name your file wrong (i.e. post.php or posts.php), CakePHP will not recognize any of your settings and will use the defaults instead.

For more on models, such as table prefixes, callbacks, and validation, check out the?Models?chapter of the Manual.

Create a Posts Controller

Next, we’ll create a controller for our posts. The controller is where all the business logic for post interaction will happen. In a nutshell, it’s the place where you play with the models and get post-related work done. We’ll place this new controller in a file called?PostsController.php?inside the?/app/Controller?directory. Here’s what the basic controller should look like:

<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">class</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nc">PostsController</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">extends</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nx">AppController</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$helpers</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Html'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Form'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">);</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span></span>

Now, lets add an action to our controller. Actions often represent a single function or interface in an application. For example, when users request www.example.com/posts/index (which is also the same as www.example.com/posts/), they might expect to see a listing of posts. The code for that action would look something like this:

<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">class</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nc">PostsController</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">extends</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nx">AppController</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$helpers</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Html'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Form'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">);</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">function</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #bf4f24;" class="nf">index</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">()</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>        <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">set</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'posts'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">find</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'all'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">));</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span></span>

Let me explain the action a bit. By defining function?index()?in our PostsController, users can now access the logic there by requesting www.example.com/posts/index. Similarly, if we were to define a function called?foobar(), users would be able to access that at www.example.com/posts/foobar.

Warning

You may be tempted to name your controllers and actions a certain way to obtain a certain URL. Resist that temptation. Follow CakePHP conventions (plural controller names, etc.) and create readable, understandable action names. You can map URLs to your code using “routes” covered later on.

The single instruction in the action uses?set()?to pass data from the controller to the view (which we’ll create next). The line sets the view variable called ‘posts’ equal to the return value of the?find('all')?method of the Post model. Our Post model is automatically available at?$this->Post?because we’ve followed Cake’s naming conventions.

To learn more about Cake’s controllers, check out the?Controllers?chapter.

Creating Post Views

Now that we have our data flowing to our model, and our application logic and flow defined by our controller, let’s create a view for the index action we created above.

Cake views are just presentation-flavored fragments that fit inside an application’s layout. For most applications they’re HTML mixed with PHP, but they may end up as XML, CSV, or even binary data.

Layouts are presentation code that is wrapped around a view, and can be defined and switched between, but for now, let’s just use the default.

Remember in the last section how we assigned the ‘posts’ variable to the view using the?set()?method? That would hand down data to the view that would look something like this:

<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">// print_r($posts) output:</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">Array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    [0] => Array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        (</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">            [Post] => Array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                (</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [id] => 1</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [title] => The title</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [body] => This is the post body.</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [created] => 2008-02-13 18:34:55</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [modified] =></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                )</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        )</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    [1] => Array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        (</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">            [Post] => Array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                (</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [id] => 2</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [title] => A title once again</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [body] => And the post body follows.</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [created] => 2008-02-13 18:34:56</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [modified] =></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                )</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        )</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    [2] => Array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        (</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">            [Post] => Array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                (</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [id] => 3</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [title] => Title strikes back</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [body] => This is really exciting! Not.</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [created] => 2008-02-13 18:34:57</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                    [modified] =></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">                )</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        )</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">)</span>

Cake’s view files are stored in?/app/View?inside a folder named after the controller they correspond to (we’ll have to create a folder named ‘Posts’ in this case). To format this post data in a nice table, our view code might look something like this:

<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"><!-- File: /app/View/Posts/index.ctp --></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"><h1>Blog posts</h1></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"><table>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    <tr>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        </span><th>Id</th>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        </span><th>Title</th>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        </span><th>Created</th>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    </span>
</tr></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    <!-- Here is where we loop through our $posts array, printing out post info --></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">foreach</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$posts</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">as</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">)</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">:</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    <tr>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        </span><td><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">echo</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">][</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'id'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">];</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"></span></span></td>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        </span><td>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">            </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">echo</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Html</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">link</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">][</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'title'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">],</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'controller'</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=></span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'posts'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'action'</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=></span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'view'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">][</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'id'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">]));</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        </span></span>
</td>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">        </span><td><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">echo</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">][</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'created'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">];</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"></span></span></td>
<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    </span>
</tr></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">endforeach</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">;</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x">    </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #007020;" class="nb">unset</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">);</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"></span></span></span></span>
</table></span>

Hopefully this should look somewhat simple.

You might have noticed the use of an object called?$this->Html. This is an instance of the CakePHP?HtmlHelperclass. CakePHP comes with a set of view helpers that make things like linking, form output, JavaScript and Ajax a snap. You can learn more about how to use them in?Helpers, but what’s important to note here is that the?link()method will generate an HTML link with the given title (the first parameter) and URL (the second parameter).

When specifying URLs in Cake, it is recommended that you use the array format. This is explained in more detail in the section on Routes. Using the array format for URLs allows you to take advantage of CakePHP’s reverse routing capabilities. You can also specify URLs relative to the base of the application in the form of /controller/action/param1/param2.

At this point, you should be able to point your browser to?http://www.example.com/posts/index. You should see your view, correctly formatted with the title and table listing of the posts.

If you happened to have clicked on one of the links we created in this view (that link a post’s title to a URL /posts/view/some_id), you were probably informed by CakePHP that the action hasn’t yet been defined. If you were not so informed, either something has gone wrong, or you actually did define it already, in which case you are very sneaky. Otherwise, we’ll create it in the PostsController now:

<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">class</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nc">PostsController</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">extends</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nx">AppController</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$helpers</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Html'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Form'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">);</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">function</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #bf4f24;" class="nf">index</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">()</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>         <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">set</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'posts'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">find</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'all'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">));</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">function</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #bf4f24;" class="nf">view</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$id</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">null</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">)</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>        <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">id</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$id</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">;</span>        <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">set</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">read</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">());</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span></span>

The?set()?call should look familiar. Notice we’re using?read()?rather than?find('all')?because we only really want a single post’s information.

Notice that our view action takes a parameter: the ID of the post we’d like to see. This parameter is handed to the action through the requested URL. If a user requests /posts/view/3, then the value ‘3’ is passed as?$id.

Now let’s create the view for our new ‘view’ action and place it in?/app/View/Posts/view.ctp:

<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"><!-- File: /app/View/Posts/view.ctp --></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"><h1></h1></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">echo</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nx">h</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">][</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'title'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">]);</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"><p><small>Created: </small></p></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">echo</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">][</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'created'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">];</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"><p></p></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">echo</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nx">h</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">][</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'body'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">]);</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp">?></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="x"></span></span></span></span>

Verify that this is working by trying the links at?/posts/index?or manually requesting a post by accessing/posts/view/1.

Adding Posts

Reading from the database and showing us the posts is a great start, but let’s allow for the adding of new posts.

First, start by creating an?add()?action in the PostsController:

<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #666666;" class="cp"><?php </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">class</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nc">PostsController</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">extends</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #c44f24;" class="nx">AppController</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$helpers</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Html'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Form'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Session'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">);</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$components</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Session'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">);</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">function</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #bf4f24;" class="nf">index</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">()</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>        <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">set</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'posts'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">find</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'all'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">));</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">function</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #bf4f24;" class="nf">view</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$id</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">)</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>        <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">id</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$id</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">;</span>        <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">set</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">,</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">read</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">());</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">}</span>    <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">public</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">function</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #bf4f24;" class="nf">add</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">()</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>        <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">if</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">request</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">is</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'post'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">))</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>            <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">create</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">();</span>            <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">if</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Post</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">save</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">request</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">data</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">))</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">{</span>                <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">Session</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">setFlash</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'Your post has been saved.'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">);</span>                <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #234aa0;" class="nv">$this</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">-></span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #080808;" class="na">redirect</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #a71d5d; font-style: italic;" class="k">array</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'action'</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #794938;" class="o">=></span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #0b6125;" class="s1">'index'</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;" class="p">));</span>   </span>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.