如何在cakephp中自定义视图2

时间:2015-03-13 14:46:16

标签: cakephp cakephp-2.0 cakephp-2.3 cakephp-2.5

我正在尝试创建一个汽车租赁网站,当客户预订汽车时我遇到了问题,必须在预订后从可用汽车的窗口拆除汽车,我有一张桌子'汽车&#39和桌子'地点',我想要的是当客户租车时,必须从汽车列表中取出汽车出租,这里是页面:/view/cars/index.ctp,/view / cars / view。 ctp,/ LocationsController.php,CarsController.php



view/cars/index.ctp : 

<div class="row">

	<?php foreach ($cars as $car):?>
 
	<div class="col-sm-6 col-md-4">
		<div class="">
			<?php echo $this->Html->link($this->Html->image($car['Car']['avatar']),
					array('action'=>'view',$car['Car']['id']),
					array('escape'=>false,'class'=>'thumbnail'));?>
			<div class="caption">
				<h5>
					<?php echo $car['Car']['title'];?>
				</h5>
				<h5> 
					Price: $
					<?php echo $car['Car']['price'];?>
				</h5>
                <h5><?php echo $this->Html->link(__('Rent a Car'), array('controller'=>'cars','action' => 'view', $car['Car']['id'])); ?></h5>
			</div>
		</div>
	</div>

	<?php endforeach;?>
  
  
  and the view/cars.view.ctp :
  
  <div class="cars view">
<h2><?php echo __('Car'); ?></h2>
    <dl>
		<dt><?php echo __('Id'); ?></dt>
		<dd>
			<?php echo h($car['Car']['id']); ?>
		</dd>
      <dt><?php echo __('Picture'); ?></dt>
		<dd>
			<?php echo h($car['Car']['picture']); ?>
		</dd>
		<dt><?php echo __('Price'); ?></dt>
		<dd>
			<?php echo h($car['Car']['price']); ?>
		</dd>
		
	</dl>
    </div>
    <?php 

echo $this->Html->image("cars/".$car['Car']['id'].".jpg", array(
    "alt" => "Cars",
    'url' => array('controller' => 'locations', 'action' => 'add', $car['Car']['id'])
));

 ?>
  
  //and the CarsController : 
  
  
  <?php
App::uses('AppController', 'Controller');
 
class CarsController extends AppController {

 
	public $components = array('Paginator', 'Session');

	public $helpers = array('Js', 'GoogleMap');


	
 public function view($id = null){
		if (!$this->Car->exists($id)) {
			throw new NotFoundException(__('Invalid car'));
		}
		
		$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
		$this->set('car', $this->Car->find('first', $options));
 		
 }

public function index() {
		$this->set('cars', $this->Car->find('all'));
	}

}


//and the LocationsController


<?php
App::uses('AppController', 'Controller');

class LocationsController extends AppController {


	
	public $components = array('Paginator', 'Session');
	public $helpers = array(
		'Js',
		'GoogleMap'
		);

	public function index() {
		$this->Location->recursive = 0;
		$this->set('locations', $this->Paginator->paginate());
	}


	public function view($id = null) {
		if (!$this->Location->exists($id)) {
			throw new NotFoundException(__('Invalid location'));
		}
		$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
		$this->set('location', $this->Location->find('first', $options));
	}


	public function add($car_id) {
		if ($this->request->is('post')) {
			$this->Location->create();
			if ($this->Location->save($this->request->data)) {
				$this->Session->setFlash(__('The location has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
			}
		}
		 $users = $this->Location->User->find('list');
		$agencies = $this->Location->Agency->find('list');
		/*$cars = $this->Location->Car->find('list');*/
		$this->set(compact('agencies'));
		$this->set('car_id', $car_id);
		
		 
		





	}


	public function edit($id = null) {
		if (!$this->Location->exists($id)) {
			throw new NotFoundException(__('Invalid location'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Location->save($this->request->data)) {
				$this->Session->setFlash(__('The location has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
			$this->request->data = $this->Location->find('first', $options);
		}
		$users = $this->Location->User->find('list');
		$agencies = $this->Location->Agency->find('list');
		$cars = $this->Location->Car->find('list');
		$this->set(compact('users', 'agencies', 'cars'));
	}


	public function delete($id = null) {
		$this->Location->id = $id;
		if (!$this->Location->exists()) {
			throw new NotFoundException(__('Invalid location'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->Location->delete()) {
			$this->Session->setFlash(__('The location has been deleted.'));
		} else {
			$this->Session->setFlash(__('The location could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
}
&#13;
&#13;
&#13;

0 个答案:

没有答案