docs:programming:php:cakephp:cakephp_notes

CakePHP

The examples below are from a working cakeblog tutorial: http://manual.cakephp.org/appendix/blog_tutorial

  • singlar: post.php in app/models/ for database table posts
  • example:
    <?php
     
    class Post extends AppModel
    {
        var $name = 'Post';
     
    	var $validate = array(
     
    		'title' => VALID_NOT_EMPTY,
    		'body'	=> VALID_NOT_EMPTY
     
    	);
    }
     
    ?>
  • plural: posts_controller.php in app/controllers for model post.php
  • functions created here are locations the user can visit
  • example:
    <?php
     
    class PostsController extends AppController
    {
        var $name = 'Posts';
     
        function index()
        {
            $this->set('posts', $this->Post->findAll());
        }
     
        function view($id = null)
        {
            $this->Post->id = $id;
            $this->set('post', $this->Post->read());
        }
     
    	function add()
    	{
    		if(!empty($this->data))
    		{
    			if($this->Post->save($this->data))
    			{
    				$this->flash('Your post has been saved.','/posts');
    			}
    		}
    	}
     
    	function delete($id)
    	{
    		$this->Post->del($id);
    		$this->flash('The post with id: '.$id.' has been deleted.', '/posts');
    	}
     
    	function edit($id = null)
    	{
    		if(empty($this->data))
    		{
    			$this->Post->id = $id;
    			$this->data = $this->Post->read();
    		}
    		else
    		{
    			if($this->Post->save($this->data['Post']))
    			{
    				$this->flash('Your post has been updated.','/posts');
    			}
    		}
     
    	}
    }
     
    ?>
  • create a folder in app/views as a plural of model: app/views/posts
  • create views for each function in posts_controller.php
  • files have exact name as function, with a .thtml extension
  • examples:
    // add.thtml
    <h1>Add Post</h1>
    <form method="post" action="<?php echo $html->url('/posts/add')?>">
    	<p>
    		Title:
    		<?php echo $html->input('Post/title', array('size' => '40'))?>
    		<?php echo $html->tagErrorMsg('Post/title', 'Title is requried.')?>
    	</p>
    	<p>
    		Body:
    		<?php echo $html->textarea('Post/body', array('rows'=>'10')) ?>
    		<?php echo $html->tagErrorMsg('Post/body', 'Body is required.') ?>
    	</p>
    	<p>
    		<?php echo $html->submit('Save') ?>
    	</p>
    </form>
     
    // edit.thtml
    <h1>Edit Post</h1>
    <form method="post" action="<?php echo $html->url('/posts/edit')?>">
    	<?php echo $html->hidden('Post/id'); ?>
    	<p>
    		Title:
    		<?php echo $html->input('Post/title', array('size' => '40'))?>
    		<?php echo $html->tagErrorMsg('Post/title', 'Title is requried.')?>
    	</p>
    	<p>
    		Body:
    		<?php echo $html->textarea('Post/body', array('rows'=>'10')) ?>
    		<?php echo $html->tagErrorMsg('Post/body', 'Body is required.') ?>
    	</p>
    	<p>
    		<?php echo $html->submit('Save') ?>
    	</p>
    </form>
     
    // index.thtml
    <h1>Blog posts</h1>
    <p><?php echo $html->link("Add Post", "/posts/add") ?></p>
    <table>
        <tr>
            <th>Id</th>
            <th>Title</th>
            <th>Created</th>
        </tr>
     
        <?php foreach ($posts as $post): ?>
        <tr>
            <td><?php echo $post['Post']['id']; ?>
    			<?php echo $html->link(
    				'Delete',
    				"/posts/delete/{$post['Post']['id']}",
    				null,
    				'Are you sure?'
    			)?>
    			<?php echo $html->link('Edit', "/posts/edit/".$post['Post']['id']); ?>
    		</td>
            <td>
                <?php echo $html->link($post['Post']['title'], "/posts/view/".$post['Post']['id']); ?>
     
            </td>
            <td><?php echo $post['Post']['created']; ?></td>
        </tr>
        <?php endforeach; ?>
     
    </table>
     
    // view.thtml
    <h1><?php echo $post['Post']['title']?></h1>
     
    <p><small>Created: <?php echo $post['Post']['created']?></small></p>
     
    <p><?php echo $post['Post']['body']?></p>
  • download and extract the most current release: http://www.cakephp.org
  • create a directory “cakephp” and place it in your /User/yourusername/Sites directory
  • move every file and folder except “app” from the extracted archive into the “cakephp”
  • move “app” to /Users/yourusername/Sites, and rename it to whatever you want your application to be titled (newApp)
  • modify newApp/.htaccess
    • after “RewriteEngine on”, add a new line: “RewriteBase /~yourusername/newApp/”
  • modify newApp/webroot/.htaccess
    • after “RewriteEngine on”, add a new line: “RewriteBase /~yourusername/newApp/”
  • copy newApp/config/database.php.default to database.php
  • modify database.php
    • in the default section, set your appropriate database connection parameters
    • for the initial setup, a tutorial for creating a blog was followed, and a basic table structure SQL statement was given
  • modify newApp/webroot/index.php
    • uncomment and modify the line: define('ROOT', DS.'Users'.DS.'yourusername'.DS.'Sites');
    • comment out the default definition for ROOT
    • uncomment and modify the line: define('APP_DIR', 'newApp');
    • comment out the default definition for APP_DIR
    • uncomment and modify the line: define('CAKE_CORE_INCLUDE_PATH', DS.'Users'.DS.'yourusername'.DS.'Sites'.DS.'cakephp');
    • comment out the default definition for CAKE_CORE_INCLUDE_PATH
  • verify that newApp/tmp is writable by your web server
  • make the default index page for the application start at a different location
    • edit config → routes.php
      ...
       
      //	$Route->connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
      	$Route->connect('/', array('controller' => 'newlocation', 'action' => 'index'));
       
      ...
  • docs/programming/php/cakephp/cakephp_notes.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1