You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PlexShare/fuel/core/classes/controller/hybrid.php

137 lines
3.0 KiB

<?php
/**
* Part of the Fuel framework.
*
* @package Fuel
* @version 1.8
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/
namespace Fuel\Core;
/**
* Hybrid Controller class
*
* A base controller that combines both templated and REST output
*
* @package Fuel
* @category Core
* @author Fuel Development Team
*/
abstract class Controller_Hybrid extends \Controller_Rest
{
/**
* @var string page template
*/
public $template = 'template';
/**
* Load the template and create the $this->template object if needed
*/
public function before()
{
// setup the template if this isn't a RESTful call
if ( ! $this->is_restful())
{
if ( ! empty($this->template) and is_string($this->template))
{
// Load the template
$this->template = \View::forge($this->template);
}
}
return parent::before();
}
/**
* router
*
* this router will call action methods for normal requests,
* and REST methods for RESTful calls
*
* @param string $resource
* @param array $arguments
* @return mixed
* @throws \HttpNotFoundException
*/
public function router($resource, $arguments)
{
// if this is an ajax call
if ($this->is_restful())
{
// have the Controller_Rest router deal with it
return parent::router($resource, $arguments);
}
// check if a input specific method exists
$controller_method = strtolower(\Input::method()) . '_' . $resource;
// fall back to action_ if no rest method is provided
if ( ! method_exists($this, $controller_method))
{
$controller_method = 'action_'.$resource;
}
// check if the action method exists
if (method_exists($this, $controller_method))
{
return call_fuel_func_array(array($this, $controller_method), $arguments);
}
// if not, we got ourselfs a genuine 404!
throw new \HttpNotFoundException();
}
/**
* After controller method has run output the template
*
* @param Response $response
*/
public function after($response)
{
// return the template if no response is present and this isn't a RESTful call
if ( ! $this->is_restful())
{
// do we have a response passed?
if ($response === null)
{
// maybe one in the rest body?
$response = $this->response->body;
if ($response === null)
{
// fall back to the defined template
$response = $this->template;
}
}
// deal with returned array's in non-restful calls
elseif (is_array($response))
{
$response = \Format::forge()->to_json($response, true);
}
// and make sure we have a valid Response object
if ( ! $response instanceof Response)
{
$response = \Response::forge($response, $this->response_status);
}
}
return parent::after($response);
}
/**
* Decide whether to return RESTful or templated response
* Override in subclass to introduce custom switching logic.
*
* @param boolean
*/
public function is_restful()
{
return \Input::is_ajax();
}
}