bench.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Fuel\Core;
  3. abstract class Controller_Bench extends \Controller_Rest
  4. {
  5. /**
  6. * @var string page template
  7. */
  8. public $template = 'template';
  9. /**
  10. * Load the template and create the $this->template object if needed
  11. */
  12. public function before()
  13. {
  14. // setup the template if this isn't a RESTful call
  15. if ( ! $this->is_restful())
  16. {
  17. if ( ! empty($this->template) and is_string($this->template))
  18. {
  19. // Load the template
  20. $this->template = \View::forge($this->template);
  21. }
  22. }
  23. return parent::before();
  24. }
  25. /**
  26. * After controller method has run output the template
  27. *
  28. * @param Response $response
  29. */
  30. public function after($response)
  31. {
  32. // return the template if no response is present and this isn't a RESTful call
  33. if ( ! $this->is_restful())
  34. {
  35. // do we have a response passed?
  36. if(empty($response))
  37. {
  38. // maybe one in the rest body?
  39. $response = $this->response->body;
  40. if (empty($response))
  41. {
  42. // fall back to the defined template
  43. $response = $this->template;
  44. }
  45. }
  46. if ( ! $response instanceof Response)
  47. {
  48. $response = \Response::forge($response);
  49. }
  50. }
  51. return parent::after($response);
  52. }
  53. /**
  54. * Decide whether to return RESTful or templated response
  55. * Override in subclass to introduce custom switching logic.
  56. *
  57. * @param boolean
  58. */
  59. public function is_restful()
  60. {
  61. return \Input::is_ajax();
  62. }
  63. }