hybrid.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Hybrid Controller class
  15. *
  16. * A base controller that combines both templated and REST output
  17. *
  18. * @package Fuel
  19. * @category Core
  20. * @author Fuel Development Team
  21. */
  22. abstract class Controller_Hybrid extends \Controller_Rest
  23. {
  24. /**
  25. * @var string page template
  26. */
  27. public $template = 'template';
  28. /**
  29. * Load the template and create the $this->template object if needed
  30. */
  31. public function before()
  32. {
  33. // setup the template if this isn't a RESTful call
  34. if ( ! $this->is_restful())
  35. {
  36. if ( ! empty($this->template) and is_string($this->template))
  37. {
  38. // Load the template
  39. $this->template = \View::forge($this->template);
  40. }
  41. }
  42. return parent::before();
  43. }
  44. /**
  45. * After controller method has run output the template
  46. *
  47. * @param Response $response
  48. */
  49. public function after($response)
  50. {
  51. // return the template if no response is present and this isn't a RESTful call
  52. if ( ! $this->is_restful())
  53. {
  54. // do we have a response passed?
  55. if(empty($response))
  56. {
  57. // maybe one in the rest body?
  58. $response = $this->response->body;
  59. if (empty($response))
  60. {
  61. // fall back to the defined template
  62. $response = $this->template;
  63. }
  64. }
  65. if ( ! $response instanceof Response)
  66. {
  67. $response = \Response::forge($response);
  68. }
  69. }
  70. return parent::after($response);
  71. }
  72. /**
  73. * Decide whether to return RESTful or templated response
  74. * Override in subclass to introduce custom switching logic.
  75. *
  76. * @param boolean
  77. */
  78. public function is_restful()
  79. {
  80. return \Input::is_ajax();
  81. }
  82. }