Let's first make sure that we have everything we need:
- installed Apache, MySQL, PHP
- PHP knowledge
- knowledge of the concept of " framework "
- knowledge of the concept of “ MVC ”
Go!
Download:Download the latest release of
Kohana 3.0 (at the time of writing: 3.0.9)
Install:Unpack the archive in any temporary folder. Open it, there should be a “kohana” directory or something like that. Rename this directory to “mykohana3” and move it to the root folder of your web server. Since I use WAMP Server - I have this “C: \ wamp \ www \”. Then open the browser “http: // localhost / mykohana3 /”. A page should appear notifying that all tests passed successfully.
If everything is in order, then delete or rename the file “install.php” in the folder “mykohana3”. Then open in the text editor “example.htaccess” and change the following line:
RewriteBase / kohana /
on:
RewriteBase / mykohana3 /
Save it as “.htaccess”.
')
Now open the file “bootstrap.php”, which is located in the “application” folder and change:
Kohana :: init ( array ( 'base_url' => '/ kohana /' ) ) ;
on:
Kohana :: init ( array ( 'base_url' => '/ mykohana3 /' ,
'index_file' => '' ) ) ;
Save this file and refresh the page in the browser. The line “hello, world!” Should appear.
Now let's start creating our first controller! Open a new document and place the following there:
<? php
defined ( 'SYSPATH' ) or die ( 'No direct script access.' ) ;
class Controller_Ko3 extends Controller
{
public function action_index ( )
{
$ this -> request -> response = 'My First Kohana 3.0 Controller' ;
}
} // End
Save it as “ko3.php” in the “application / classes / controller” folder. Open the browser “http: // localhost / mykohana3 / ko3 ″. You should see “My First Kohana 3.0 Controller” on the screen.
Now go through the code.
defined ( 'SYSPATH' ) or die ( 'No direct script access.' ) ;
The line above prohibits accessing the file directly. It can only be called from the framework.
class Controller_Ko3 extends Controller
Here a controller is created, which is a class that inherits Controller - the built-in framework class.
public function action_index ( )
This is where the “action_index” method is created. It is started by the default framework when the controller is called. Just like index.php starts when you open a site in a browser.
$ this -> request -> response = 'My First Kohana 3.0 Controller' ;
This will display the “My First Kohana 3.0 Controller”. Essentially works like “echo”.
It's pretty easy now, right? Now, if you want to add another action to the controller, you need to create a public method with the prefix “action_”. To access it, open the “http: // localhost / mykohana3 / {controller} / {action}”
Let's create a new method in the “ko3 ″ controller by adding the following block after the“ action_index ”method:
public function action_another ( )
{
$ this -> request -> response = 'Another action' ;
}
Save the file and open in the browser “http: // localhost / mykohana3 / ko3 / another”. The message “Another action” should appear.
Now add some interactivity!
Copy this code and paste it after the “action_another” method:
public function action_dynamic ( $ say )
{
$ this -> request -> response = 'You said:' . $ say ;
}
Save the file and open the browser “http: // localhost / mykohana3 / ko3 / dynamic / Monkey”. “You said: Monkey” should appear.
P.S:
This is a translation of someone else's material.
This is a translation of someone else’s material by a person who is just beginning to learn the framework.
This is only the first part of dating.