Skip to content

Controller

Viames Marino edited this page Jun 15, 2018 · 1 revision

The controller, according to the MVC paradigm, manages the user input and communicates to the model the actions to be performed on the data. Some best practices suggest keeping the controller small by moving all operations and data management logic into the model.

The following is an essential controller that performs CRUD operations for the Person object, which inherits from ActiveRecord.

<?php

// Pair namespace
use Pair\Breadcrumb;
use Pair\Controller;

class PersonsController extends Controller {
	
	/**
	 * Initialize the Breadcrumb.
	 * {@inheritDoc}
	 *
	 * @see Pair\Controller::init()
	 */
	protected function init() {
		
		// breadcrumb is now / Home / Persons
		$breadcrumb = Breadcrumb::getInstance();
		$breadcrumb->addPath('Persons', 'persons');
		
	}
	
	/**
	 * Add a new object.
	 */
	public function addAction() {
	
		// create an empty Person object
		$person = new Person();

		// now populate it with all submitted form inputs
		$person->populateByRequest();

		// write a new db record matching the object
		$result = $person->store();
		
		if ($result) {

			// notify the successful creation and redirect
			$this->enqueueMessage($this->lang('PERSON_HAS_BEEN_CREATED'));
			$this->redirect('persons');

		} else {

			// notify the errors
			$msg = $this->lang('PERSON_HAS_NOT_BEEN_CREATED') . ':';
			foreach ($person->getErrors() as $error) {
				$msg .= " \n" . $error;
			}
			$this->enqueueError($msg);
			$this->view = 'default';

		}					

	}

	/**
	 * Show form for edit a Person object.
	 */
	public function editAction() {
	
		// check if requested object is valid
		$person = $this->getObjectRequestedById('Person');
		
		// if object is valid, proceed to show the form
		$this->view = $person ? 'edit' : 'default';
	
	}

	/**
	 * Modify a Person object.
	 */
	public function changeAction() {

		// load the requested Person object from db
		$person = new Person(Input::get('id'));

		// update it with all submitted form inputs
		$person->populateByRequest();

		// apply the update
		$result = $person->store();

		if ($result) {

			// notify the change and redirect
			$this->enqueueMessage($this->lang('PERSON_HAS_BEEN_CHANGED_SUCCESFULLY'));
			$this->redirect('persons');

		} else {

			// get error list from object
			$errors = $person->getErrors();

			if (count($errors)) { 
				$message = $this->lang('ERROR_ON_LAST_REQUEST') . ": \n" . implode(" \n", $errors);
				$this->enqueueError($message);
				$this->view = 'default';
			} else {
				$this->redirect('persons');
			}

		}

	}

	/**
	 * Delete a Person object.
	 */
	public function deleteAction() {

		// get the requested object by URL parameter
	 	$person = $this->getObjectRequestedById('Person');

		// execute deletion
		$result = $person->delete();

		if ($result) {

			// notify the deletion and redirect
			$this->enqueueMessage($this->lang('PERSON_HAS_BEEN_DELETED_SUCCESFULLY'));
			$this->redirect('persons');

		} else {

			// get error list from object
			$errors = $person->getErrors();

			if (count($errors)) { 
				$message = $this->lang('ERROR_DELETING_PERSON') . ": \n" . implode(" \n", $errors);
				$this->enqueueError($message);
				$this->view = 'default';
			} else {
				$this->enqueueError($this->lang('ERROR_ON_LAST_REQUEST'));
				$this->redirect('persons');
			}

		}

	}

}
Clone this wiki locally