- Programming with CodeIgniterMVC
- Eli Orr Yehuda Zadik
- 350字
- 2021-08-06 16:48:04
Example 1 – hello world
Initially, we will start with a simple example that displays Hello World on the rendered web page. This is an example that doesn't use a database.
The URI will be http://ourdomain.com/index.php/hello
.
We can eliminate the index.php
file from the path to enable a shorter URI; that is, http://ourdomain.com/index.php/hello
.
In order to enable these shorter URIs, we will make configuration changes as described in Chapter 2, Configurations and Naming Conventions, regarding the config.php
index_page setting in config.php
.
We will build the following two scripts:
- Controller class:
application/controllers/hello.php
- View script:
application/views/helloview.php
In this example, we use the default configuration. For more information about configurations, refer to Chapter 2, Configurations and Naming Conventions. The controller in this example passes the parameters that are displayed in the view.
Tip
Passing the parameters from the controller to the view is optional.
The controller file
Here's the code sample of the controller. The controller is responsible for rendering the view with the parameters, such as mega title and message. For naming the controller classes, refer to Chapter 2, Configurations and Naming Conventions.
<?php
class Hello extends CI_Controller {
* Index Page for this controller.
* Maps to the following URLhttp://example.com/index.php/hello
- or - http://example.com/index.php/hello/index- or -* since this controller is set as the default controller in config/routes.php, it's displayed at http://example.com/
* So any other public methods not prefixed with an underscorewill map to /index.php/welcome/<method_name>
@see http://codeigniter.com/user_guide/general/urls.html
public function index(){
// Note that $view_params is optional// we can use $this->load->view('helloview');as well.// if the view doesn't use php variables
// The $view_params is extracted in the view script to php// variables $key = $value
// In this example three variables will be generated by CI in the // view page
// helloview.php variable: $mega_title
// value: 'Codeigniter - Hello World' // variable: $title value: 'Welcome to // Codegniter'// variable: $message value: 'Hello World'
$view_params = array(
'mega_title' => 'Codeigniter - Hello World', 'title' => 'Welcome to Codegniter','message' => 'Hello World' );
$this->load->view('helloview', $view_params); }
} // closing the class definition
/* End of file welcome.php *//* Location: ./application/controllers/welcome.php */
Tip
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The view file
The following is the corresponding rendered view that uses the parameters provided by the controller to render the view to the web page and return it to the user:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title><?php echo $mega_title ?></title> </head> <body> <div id="container"> <h1><?php echo $title ?></h1> <div id="body"> <p><?php echo $message ?></p> </div></div> </body> </html>
- 圖解Java數(shù)據(jù)結(jié)構(gòu)與算法(微課視頻版)
- Mastering PHP Design Patterns
- 算法精粹:經(jīng)典計(jì)算機(jī)科學(xué)問(wèn)題的Python實(shí)現(xiàn)
- 新一代SDN:VMware NSX 網(wǎng)絡(luò)原理與實(shí)踐
- 鴻蒙OS應(yīng)用編程實(shí)戰(zhàn)
- PrimeFaces Blueprints
- Python自然語(yǔ)言理解:自然語(yǔ)言理解系統(tǒng)開(kāi)發(fā)與應(yīng)用實(shí)戰(zhàn)
- 高效使用Greenplum:入門(mén)、進(jìn)階與數(shù)據(jù)中臺(tái)
- Maven for Eclipse
- 零基礎(chǔ)學(xué)Java第2版
- 從零開(kāi)始學(xué)Python大數(shù)據(jù)與量化交易
- Building Web and Mobile ArcGIS Server Applications with JavaScript(Second Edition)
- 零基礎(chǔ)入門(mén)學(xué)習(xí)C語(yǔ)言:帶你學(xué)C帶你飛
- Neo4j Graph Data Modeling
- 算法學(xué)習(xí)與應(yīng)用從入門(mén)到精通