PHP Kohana Introductory Experience Tutorial

Source: Internet
Author: User
Tags kohana

Description
1, the text appears in an English string followed by a slash "/" words that it is a folder.
2, if an English string appears in front of "/" in the root directory of the Web space, the general file Plus I do not add "/" in front, unless it is more than 2 layers of the directory.
3, put Kohana unpack the appropriate location, I was directly placed in the root directory of the Web space, if placed in other locations need to modify the URL, you need to let Apache find Kohana index.php file.

Kohana is divided into two folders, system/and application/, and a/index.php file. where system/and/index.php best not to change.
The following steps, if not specifically noted, occur in application/.


1, in the browser input localhost/index.php can see the default debugging with the home page (this home does not mean Kohana in the index.php, just the visual effect on the home).
The displayed text prompts the controller location to control output to the user's home page: application/controllers/welcome.php.

2. Add a new function to the class in the file/application/controllers/welcome.php given by Kohana, which is the name of the page that will be accessed. For example, the default function index () is equivalent to the default index.php. You can also add other functions, such as adding a function hello ():

PHP Copy Code<?php
function Hello() {
echo "See, my role is to control the integration of the data required by the ' Hello ' page with the output appearance template." <br> output format do not set in my here, it is best to set up a special xxx_view.php file under/application/view. (As long as you identify, whatever name you can, usually the controller file in the corresponding function name plus a suffix _view, easy to find templates.) CI below do not know is not so casual, I am not sure, hehe. <br> the text you see is the content, and the line you see is the look. In general, the content and appearance will not appear in my case, the content should be placed in the database or other files. <br> Well, so although I am ugly now, I have finished the integration of data and appearance, and is very not in line with the MVC specification of the data and appearance are in my file, hehe. ";
}
?> Copy Code

Enter Localhost/index.php/welcome/hello in the navigator to see the contents of the above echo.
index.php in the URL can also be removed, but requires Apache's rewrite support.

3. Modify the default home page to point to the new file aaa.php.
Copy the/system/config/routes.php to the/application/config/. The value of the _default in the new routes.php is changed to AAA.
The modification here is equivalent to setting the name of a file to index.html or index.php under the Apache server and selecting the file when you enter the folder without specifying a specific file. Another Controllers commonly used function _default () is the function that is called when the wrong function is called (in this case, the function method), typically in a function that points to the 404 error page.
In addition, we can see that if the file in the application/has the same name as the file in sysytem/, the former has a higher priority.
The _default set in/application/config/routes.php is the application folder and the system folder set in/index.php [can be set autonomously in/index.php, not necessarily application and System] As the root directory, the files with the same name as the application folder and the System folder path are selected as preference objects in the application folder.

4. Name the class name aaa.php the same as the file and write the first letter in uppercase, i.e. AAA. Otherwise, the call to the function cannot be completed.

PHP Copy Code<?php
The suffix _controller here must be added, thanks to the moderator timely correction.
Class Aaa_controllerextends controller  {             
     Function index ()  {
          echo  "I am the new homepage, OH also ~"         
        function  page1 ()  {
           echo  "Did you find me?" Know how to look at me, haha! <br> Next we'll use view to add a look. <br> Although the data is still in my file, I'm going to take an extremely important step towards the MVC pattern-separating the look from the file I'm in! "         }
  }
  ?> Copy Code

Page Page1 can be accessed through localhost/index.php/aaa/page1. Give it a try, please.
The relationship between Class AAA and Function page (), Funciton index () is like the relationship between a folder and its files.

5, nested template, in/applincation/view to establish a file page_view.php.
First, write a static HTML file, page_view.php. Don't ask me why my suffix is. php, my content can be all static html! Joking, actually is kohana only recognize. PHP end of the file, if you want to add another view file to configure the Kohana.

HTML Copy Code<HTML>
<head>
</head>
<body>
<H1> Title number </H1>
<hr><hr>
<p> content number one. Of course I can write more, but I'm too lazy to write--~</P>
</body>
</HTML> Copy Code

You can open the browser to see the URL of the above file directly, convenient and behind the comparison.

The place where the dynamic content may appear in the PHP output, emphasizing, just PHP output, the purpose is to show the model obtained, processed data.

HTML Copy Code<HTML>
<head>
</head>
<body>
<H1><? = $title?></H1>
<hr><hr>
<p><? = $contont?></P>
</body>
</HTML> Copy Code

It's strange how $title and $content got here. Well, because I haven't modified the controller file (ie,/application/controllers/aaa.php) and added related variables, so your strange can understand, hehe ~
OK, let's modify/application/controllers/aaa.php, add function Page2 () to go in.

PHP Copy Code<?php
Class Aaa_controllerExtends Controller{
Function index(){
Echo"I am the new homepage, OH also ~";
}
Function Page1(){
Echo"Have you found me?" Know how to look at me, haha! <br> Next we'll use view to add a look. <br> Although the data is still in my file, I'm going to take an extremely important step towards the MVC pattern-separating the look from the file I'm in! ";
}
Page2 (),, I ' m here!
function Page2(){
$view=$this-Load-View(' Page_view ');1, do not explain the origin of the $this->load->view, anyway you have to get used to this kind of file (or function) call method, often used later, 2, Page_view is the function page () corresponding template file View file name, This is the template file name.
$viewtitle = "Heading No. 2nd";
$viewc;
$view, Render(TRUE);
}
//end of Page2 ()
}

?> Copy Code

Now there are three pages, you can change to see.

The use of $view properties to pass parameters although simple and clear, but always feel a little bit of awkward, the data is data, it is best not to and $view controller pull together.
OK, then we'll change the way. In/application/controllers/aaa.php, add the following content. [This paragraph is intercepted from the CI tutorial, but look at another post [http://codeigniter.org.cn/forums/thread-99-1-1.html's four floors] It is said that the Kohana controller (controler) has a new way of passing parameters to the Views (view) $this->layout->var1 = "; $this->layout->view (' Welcome '); , but I haven't used it yet (I'm a new bird too.) )]

PHP Copy Code<?php
function Page3(){
$data[' Title ']="Heading No. 3rd";
$data[' Content ']="Content number 3rd. See, I was using\ $data Array to pass the data! <br> as you can see, I'm in\ $this->load->view () added the parameter \ $data ";
$view = load, $this, view(' Page_view ', $data);

$view, Render(TRUE);
}
}
?> Copy Code

6. Using PHP loops in the template, add the following code to the page_view.php:

PHP Copy Code<?phpForeach($names as $name ) : ?>
<li><?=$name?></li>
<?php Endforeach ?>

can also use the following way, just a personal hobby, hehe.
<?php foreache($names as $name ) { ?>
<li><?=$name?></li>
<?php } ?> Copy Code

Add a test array to the/application/controllers/aaa.php.

PHP Copy Code<?php
$names = array(' Adam ', ' Bruce ', ' Danna ');
?> Copy Code

PHP Kohana Introductory Experience Tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.