PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/user_guide_src/source/tutorial/static_pages.rst

https://gitlab.com/atoz-chevara/CodeIgniter
ReStructuredText | 170 lines | 127 code | 43 blank | 0 comment | 0 complexity | b2b5e3e1203981eb54497a20e76b4fea MD5 | raw file
  1. ############
  2. Static pages
  3. ############
  4. **Note:** This tutorial assumes you've downloaded CodeIgniter and
  5. :doc:`installed the framework <../installation/index>` in your
  6. development environment.
  7. The first thing you're going to do is set up a **controller** to handle
  8. static pages. A controller is simply a class that helps delegate work.
  9. It is the glue of your web application.
  10. For example, when a call is made to:
  11. http://example.com/news/latest/10
  12. We might imagine that there is a controller named "news". The method
  13. being called on news would be "latest". The news method's job could be to
  14. grab 10 news items, and render them on the page. Very often in MVC,
  15. you'll see URL patterns that match:
  16. http://example.com/[controller-class]/[controller-method]/[arguments]
  17. As URL schemes become more complex, this may change. But for now, this
  18. is all we will need to know.
  19. Create a file at *application/controllers/Pages.php* with the following
  20. code.
  21. ::
  22. <?php
  23. class Pages extends CI_Controller {
  24. public function view($page = 'home')
  25. {
  26. }
  27. }
  28. You have created a class named ``Pages``, with a view method that accepts
  29. one argument named ``$page``. The ``Pages`` class is extending the
  30. ``CI_Controller`` class. This means that the new pages class can access the
  31. methods and variables defined in the ``CI_Controller`` class
  32. (*system/core/Controller.php*).
  33. The **controller is what will become the center of every request** to
  34. your web application. In very technical CodeIgniter discussions, it may
  35. be referred to as the *super object*. Like any php class, you refer to
  36. it within your controllers as ``$this``. Referring to ``$this`` is how
  37. you will load libraries, views, and generally command the framework.
  38. Now you've created your first method, it's time to make some basic page
  39. templates. We will be creating two "views" (page templates) that act as
  40. our page footer and header.
  41. Create the header at *application/views/templates/header.php* and add
  42. the following code:
  43. ::
  44. <html>
  45. <head>
  46. <title>CodeIgniter Tutorial</title>
  47. </head>
  48. <body>
  49. <h1><?php echo $title; ?></h1>
  50. The header contains the basic HTML code that you'll want to display
  51. before loading the main view, together with a heading. It will also
  52. output the ``$title`` variable, which we'll define later in the controller.
  53. Now, create a footer at *application/views/templates/footer.php* that
  54. includes the following code:
  55. ::
  56. <em>&copy; 2015</em>
  57. </body>
  58. </html>
  59. Adding logic to the controller
  60. ------------------------------
  61. Earlier you set up a controller with a ``view()`` method. The method
  62. accepts one parameter, which is the name of the page to be loaded. The
  63. static page templates will be located in the *application/views/pages/*
  64. directory.
  65. In that directory, create two files named *home.php* and *about.php*.
  66. Within those files, type some text − anything you'd like − and save them.
  67. If you like to be particularly un-original, try "Hello World!".
  68. In order to load those pages, you'll have to check whether the requested
  69. page actually exists:
  70. ::
  71. public function view($page = 'home')
  72. {
  73. if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
  74. {
  75. // Whoops, we don't have a page for that!
  76. show_404();
  77. }
  78. $data['title'] = ucfirst($page); // Capitalize the first letter
  79. $this->load->view('templates/header', $data);
  80. $this->load->view('pages/'.$page, $data);
  81. $this->load->view('templates/footer', $data);
  82. }
  83. Now, when the page does exist, it is loaded, including the header and
  84. footer, and displayed to the user. If the page doesn't exist, a "404
  85. Page not found" error is shown.
  86. The first line in this method checks whether the page actually exists.
  87. PHP's native ``file_exists()`` function is used to check whether the file
  88. is where it's expected to be. ``show_404()`` is a built-in CodeIgniter
  89. function that renders the default error page.
  90. In the header template, the ``$title`` variable was used to customize the
  91. page title. The value of title is defined in this method, but instead of
  92. assigning the value to a variable, it is assigned to the title element
  93. in the ``$data`` array.
  94. The last thing that has to be done is loading the views in the order
  95. they should be displayed. The second parameter in the ``view()`` method is
  96. used to pass values to the view. Each value in the ``$data`` array is
  97. assigned to a variable with the name of its key. So the value of
  98. ``$data['title']`` in the controller is equivalent to ``$title`` in the
  99. view.
  100. Routing
  101. -------
  102. The controller is now functioning! Point your browser to
  103. ``[your-site-url]index.php/pages/view`` to see your page. When you visit
  104. ``index.php/pages/view/about`` you'll see the about page, again including
  105. the header and footer.
  106. Using custom routing rules, you have the power to map any URI to any
  107. controller and method, and break free from the normal convention:
  108. ``http://example.com/[controller-class]/[controller-method]/[arguments]``
  109. Let's do that. Open the routing file located at
  110. *application/config/routes.php* and add the following two lines.
  111. Remove all other code that sets any element in the ``$route`` array.
  112. ::
  113. $route['default_controller'] = 'pages/view';
  114. $route['(:any)'] = 'pages/view/$1';
  115. CodeIgniter reads its routing rules from top to bottom and routes the
  116. request to the first matching rule. Each rule is a regular expression
  117. (left-side) mapped to a controller and method name separated by slashes
  118. (right-side). When a request comes in, CodeIgniter looks for the first
  119. match, and calls the appropriate controller and method, possibly with
  120. arguments.
  121. More information about routing can be found in the URI Routing
  122. :doc:`documentation <../general/routing>`.
  123. Here, the second rule in the ``$routes`` array matches **any** request
  124. using the wildcard string ``(:any)``. and passes the parameter to the
  125. ``view()`` method of the ``Pages`` class.
  126. Now visit ``index.php/about``. Did it get routed correctly to the ``view()``
  127. method in the pages controller? Awesome!