如何在子文件夹PHP中呈现视图

前端之家收集整理的这篇文章主要介绍了如何在子文件夹PHP中呈现视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是MVC的新手,刚开始练习它.我正在创建一个中小型企业网站,并且不想使用任何大型框架,所以我找到了 this,到目前为止它工作得很好.我似乎唯一不理解的是如何在子文件夹中呈现视图.

我有3种药物需要显示信息,它们的结构如下:

  1. views
  2. --medicines
  3. ----medicine1
  4. ------info.PHP
  5. ------forms
  6. --------male.PHP
  7. --------female.PHP

这是药物控制器:

  1. <?PHP
  2.  
  3. class MedicinesController extends Controller
  4. {
  5. /**
  6. * Construct this object by extending the basic Controller class
  7. */
  8. public function __construct()
  9. {
  10. parent::__construct();
  11.  
  12. Auth::checkAuthentication();
  13. }
  14.  
  15. /**
  16. * Handles what happens when user moves to URL/medicines/index
  17. **/
  18. public function index()
  19. {
  20. $this->View->render('medicines/index');
  21. }
  22.  
  23. /**
  24. * Handles what happens when user moves to URL/medicines/medicine1
  25. **/
  26. public function medicine1()
  27. {
  28. $this->View->render('medicines/medicine/info',array(
  29. 'files' => FilesModel::getMedicineFiles())
  30. );
  31. }
  32.  
  33. /**
  34. * Handles what happens when user moves to URL/medicines/medicine1/forms/male
  35. **/
  36. public function male()
  37. {
  38. $this->View->render('medicines/imnovid/forms/male');
  39. }
  40.  
  41. }

这是处理控制器的类:

  1. /**
  2. * Class Application
  3. * The heart of the application
  4. */
  5. class Application
  6. {
  7. /** @var mixed Instance of the controller */
  8. private $controller;
  9.  
  10. /** @var array URL parameters,will be passed to used controller-method */
  11. private $parameters = array();
  12.  
  13. /** @var string Just the name of the controller,useful for checks inside the view ("where am I ?") */
  14. private $controller_name;
  15.  
  16. /** @var string Just the name of the controller's method,useful for checks inside the view ("where am I ?") */
  17. private $action_name;
  18.  
  19. /**
  20. * Start the application,analyze URL elements,call according controller/method or relocate to fallback location
  21. */
  22. public function __construct()
  23. {
  24. // create array with URL parts in $url
  25. $this->splitUrl();
  26.  
  27. // creates controller and action names (from URL input)
  28. $this->createControllerAndActionNames();
  29.  
  30. // does such a controller exist ?
  31. if (file_exists(Config::get('PATH_CONTROLLER') . $this->controller_name . '.PHP')) {
  32.  
  33. // load this file and create this controller
  34. // example: if controller would be "car",then this line would translate into: $this->car = new car();
  35. require Config::get('PATH_CONTROLLER') . $this->controller_name . '.PHP';
  36. $this->controller = new $this->controller_name();
  37.  
  38. // check for method: does such a method exist in the controller ?
  39. if (method_exists($this->controller,$this->action_name)) {
  40. if (!empty($this->parameters)) {
  41. // call the method and pass arguments to it
  42. call_user_func_array(array($this->controller,$this->action_name),$this->parameters);
  43. } else {
  44. // if no parameters are given,just call the method without parameters,like $this->index->index();
  45. $this->controller->{$this->action_name}();
  46. }
  47. } else {
  48. // load 404 error page
  49. require Config::get('PATH_CONTROLLER') . 'ErrorController.PHP';
  50. $this->controller = new ErrorController;
  51. $this->controller->error404();
  52. }
  53. } else {
  54. // load 404 error page
  55. require Config::get('PATH_CONTROLLER') . 'ErrorController.PHP';
  56. $this->controller = new ErrorController;
  57. $this->controller->error404();
  58. }
  59. }
  60.  
  61. /**
  62. * Get and split the URL
  63. */
  64. private function splitUrl()
  65. {
  66. if (Request::get('url')) {
  67.  
  68. // split URL
  69. $url = trim(Request::get('url'),'/');
  70. $url = filter_var($url,FILTER_SANITIZE_URL);
  71. $url = explode('/',$url);
  72.  
  73. // put URL parts into according properties
  74. $this->controller_name = isset($url[0]) ? $url[0] : null;
  75. $this->action_name = isset($url[1]) ? $url[1] : null;
  76.  
  77. // remove controller name and action name from the split URL
  78. unset($url[0],$url[1]);
  79.  
  80. // rebase array keys and store the URL parameters
  81. $this->parameters = array_values($url);
  82. }
  83. }
  84.  
  85. /**
  86. * Checks if controller and action names are given. If not,default values are put into the properties.
  87. * Also renames controller to usable name.
  88. */
  89. private function createControllerAndActionNames()
  90. {
  91. // check for controller: no controller given ? then make controller = default controller (from config)
  92. if (!$this->controller_name) {
  93. $this->controller_name = Config::get('DEFAULT_CONTROLLER');
  94. }
  95.  
  96. // check for action: no action given ? then make action = default action (from config)
  97. if (!$this->action_name or (strlen($this->action_name) == 0)) {
  98. $this->action_name = Config::get('DEFAULT_ACTION');
  99. }
  100.  
  101. // rename controller name to real controller class/file name ("index" to "IndexController")
  102. $this->controller_name = ucwords($this->controller_name) . 'Controller';
  103. }
  104. }

配置

  1. /**
  2. * Configuration for: Folders
  3. * Usually there's no reason to change this.
  4. */
  5. 'PATH_CONTROLLER' => realpath(dirname(__FILE__).'/../../') . '/application/controller/','PATH_VIEW' => realpath(dirname(__FILE__).'/../../') . '/application/view/',/**
  6. * Configuration for: Default controller and action
  7. */
  8. 'DEFAULT_CONTROLLER' => 'index','DEFAULT_ACTION' => 'index',

和渲染

  1. public function render($filename,$data = null)
  2. {
  3. if ($data) {
  4. foreach ($data as $key => $value) {
  5. $this->{$key} = $value;
  6. }
  7. }
  8.  
  9. require Config::get('PATH_VIEW') . '_templates/header.PHP';
  10. require Config::get('PATH_VIEW') . $filename . '.PHP';
  11. require Config::get('PATH_VIEW') . '_templates/footer.PHP';
  12. }

编辑

如果我做var_dump();我得到这个输出

  1. D:\Programs\wamp64\www\ermp.ee\application\core\Application.PHP:84:
  2. object(Application)[3]
  3. private 'controller' => null
  4. private 'parameters' =>
  5. array (size=2)
  6. 0 => string 'forms' (length=5)
  7. 1 => string 'male' (length=4)
  8. private 'controller_name' => string 'medicines' (length=9)
  9. private 'action_name' => string 'imnovid' (length=7)
简单的网址

当您访问http://ermp.ee/medicines/时,它会呈现application / view / medicines / index.PHP文件,原因如下:

  1. $this->View->render('medicines/index');

当您访问http://ermp.ee/medicines/medicine1/forms/male时,它会使用文件参数呈现application / view / medicines / info.PHP文件

  1. $this->View->render('medicines/medicine/info'...

您将视图文件名直接传递给View.表单和男性存储在Application的私有属性参数中.

在这种情况下,controller_name是MedicinesController,action_name是medicine1.

与男性的网址

当您访问http://ermp.ee/medicines/male时,action_name是男性,但由于以下行,呈现的视图是药品/ imnovid / forms / male.PHP

  1. $this->View->render('medicines/imnovid/forms/male');

无论视图的路径是什么,都从URI获取action_name.根据动作名称使用路径只是一种惯例,您可以随意渲染任何您想要的内容.

猜你在找的PHP相关文章