Spawn Framework · Contact · Community · Tutorial · Documentation · Download · About

Learn :: Controller

Ok, lets start:
create Home.php file in /Application/Controller/ folder.

Open this file in your editor and create home controller:
<?php
//namespace name
namespace Controller;
//controller name
class Home extends \Spawn\Controller 
{
   //first action
   public function indexAction()
   {
       $this -> response = 'Hello World!';
   }
}
Now open http://yoursite.com/home or http://yoursite.com/home/index in browser :)
If we need open other action in browser:
<?php
namespace Controller;
//controller name
class Home extends \Spawn\Controller
{
   //first action
   public function indexAction()
   {
       $this -> response = 'Hello World!';
   }

   //next action
   public function testAction()
   {
       $this -> response = 'test!';
   }
}
Now open http://yoursite.com/home/test in browser :)

If we need use any model in all actions we can use special method: init() and end();
<?php
namespace Controller;
//controller name
class Home extends \Spawn\Controller
{
   public function init()
   {
       //'I am init() method and I\'m start before action :-)';
       $this -> method = new \Model\Foo();
   }

   //first action
   public function indexAction()
   {
       $this -> response = 'Hello World!';
   }

   //next action
   public function testAction()
   {
       $this -> response = 'test!';
   }

   public function end()
   {
       $this -> method -> bar();
   }
}

If we need set home controller at start controller when user no get any name after mysite.com.
Open index.php file
We see:
//line 31,32
$spawn -> controller = 'Spawn';
$spawn -> action = 'index';
Here we have controller and action names to use when before controller/action not found or not exists.
Replace $spawn -> controller to $spawn -> controller = 'home' and save file.
Now when user comes to your site first controller and action to load
is controller Home and action indexAction().