Demo entry 6628421

a controller_demo of cakephp

   

Submitted by anonymous on Jul 04, 2017 at 06:34
Language: PHP. Code size: 1.2 kB.

<?php

Class PostsController extends AppController{

	var $name = 'Posts';

	

	public function index(){
		header("Content-type:text/html;charset=utf-8");

		$this->set('posts', $this->Post->find('all'));		
	
	}

	public function view($id=null){
		$this->Post->id = $id;
		$this->set('posts', $this->Post->read());
	
	}

	public function add(){
		if(!empty($this->data)){
			if($this->Post->save($this->data)){
				$this->Session->setFlash('Your post has been saved!');
				$this->redirect(array('action'=>'index'));
			
			}
		
		
		}
	
	}

	function delete($id) {
		if($this->Post->del($id)){
			$this->Session->setFlash('The post with id: '.$id.' has been deleted.');
			$this->redirect(array('action'=>'index'));
		}else{
			echo $id.'------<br />';
			echo '<script>alert(\'删除失败\')</script>';
			$this->redirect(array('action'=>'index'));
		}
	}

	function edit($id = null) {
		$this->Post->id = $id;
		if (empty($this->data)) {
			$this->data = $this->Post->read();
		} else {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash('Your post has been updated.');
				$this->redirect(array('action' => 'index'));
			}
		}
	}



}
?>

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).