Tutorial 2, Adding your own methods

So you finish the 1st tutorial or you want to quickly start to use Tequila and you are wondering where to add your own methods?

Tequila is quite simple to use, you just have to follow some basic rules that we will review in a simple case (no actual coding)

Case:

Create a page that shows a calendar view of my activities

Create the required files:

* If you add this as a method to an existing class you don't need to create files, the easier way is to copy them from an existing one

  • Controller: app/controllers/tutorial.php

  • Model: app/model/tutorial_model.php

  • view: app/views/tutorial_view.php

  • VO: app/VO/activities_VO.php

  • DAO: -- We will not use a db for this sample

  • Language: languages/EN/tutorial.php

  • Template: templates/yourstyle/tutorial.php

Sure that's a lot of files, but you can copy from another sample, or you can just call:

Automatic file creation

index.php?task=addpage

And select the last option: Generate an empty page (no DB related)

Now lets start coding...

Adding the controller methods

For every method we want the controller to respond, just add a function with that name.

The function can receive the $model and $view or you can access them from the class using : $this->model and $this->view

// controller 2 protected function test(&$model, &$view) 3 { 4 $month = safeGet('month', month()); 5 $data = $model->getsample($month); 6 $view->show_month($data); 7 }

In this function we just assume:

- We will have a parameter called month, or the current month

- We will create a public function in the model called getsample

- We will create a public function in the view called show_month

Want to make this function the default?

set the class var $mode

1 protected $mode = 'test';

Browsing to your new method

You can access this method by calling:

index.php?task=tutorial&mode=test

* As you can see the controller is quite easy to code, if you already know all the calls your user / client will make to the system, you can add all the methods at once

Adding the model code

The model class is a normal standard PHP class, it doesn't have to inherit from any other class, this allow you to use any class you already made in the past or any open source class.

*Your class should be a real model class, no interaction with post,get,session and no output, echo, smarty etc

In this class you will have your database calls, file access or any business rule you need to implement, your class might return a result (preferred method) or can hold its own status with methods to be called by the view.

In this case we will mock up some data and return a month array with an array of activities per day

This code is irrelevant, just notice:

    • Function is public

    • Function name matches the call in controller

    • Function get all external info in parameters

    • No Post/Get/Session data is accessed

    • Function return arrays of VO's this is tequila preferred data

1 2 // model 3 public function getsample($month) 4 { 5 $lastdayofmonth = 31;//some php function to get the last day of param month 6 // make some fake data 7 8 $tmp = array(); 9 for ($q=0; $q<$lastdayofmonth; $q++) 10 { 11 $dayactivities = array(); 12 $t_vo = new activities_VO(); 13 $t_vo->day = $q; 14 $t_vo->date = year().month().$q; 15 $t_vo->action = "Invented action for $q"; 16 $t_vo->projname = " Proj for day $q"; 17 $dayactivities[] = $t_vo; 18 //Create another activity and add 19 $dayactivities[] = $t_vo; 20 $tmp[$q] = $dayactivities; 21 } 22 return $tmp; 23 24 }

There we go an array of activities_VO's with fake data..

In you case you might want to do something more interesting you can check Data page or The model pages to see how to work with data.

Adding the view code

So far so good, what's next? We already handle the call and process the information in the model, now we will add in the view a public method called show_month

In this method you could simply add a function in the view called show_month and start printing the results, in Tequila what we do is we start calling the template engine to add the regions and values that we want.

If you are not sure on how work with templates check this, check template page

The sample code that follows is just a sample, but notice:

    • Function matches the call from the controller

    • Data is normally passed to the function

    • More info from the model can be accessed using $this->model !!

    • The view has no ECHO, Print methods, all is render trough templates

// VIEW 2 public function show_month($data) 3 { 4 $lastdayofmonth = 30;//PHP function or USE just array data; 5 for ($q=1; $q<$lastdayofmonth; $q++) 6 { 7 $this->template->newBlock("day"); 8 9 $this->template->assign("dayno", $q); 10 foreach ($data[$q] as $dayVO) 11 { 12 $this->template->assign('action', $dayVO->action); 13 //assign any other properties 14 } 15 } 16 }

Simplifying the view.

You can always simplify the view by using an strategy or making your own See strategies

Strategies provide a fantastic way of reduce coding without loosing any flexibility.

The code then will be:

1 public function show_month($data) 2 { 3 $v = new view_calendarized($this->template, $data); 4 $v->getview(); 5 }

I need JSON / XML data, how to?

Tequila provide a strategy to make XML data from a collection of objects an arrays, you just need to call the right strategy and change the RRT (View: Requested Response Type)

XML Sample:

1 public function show_month($data) 2 { 3 $v = new view_xml_data($this->template, $data); 4 echo $v->getview(); 5 }

JSON Sample:

1 public function show_month($data) 2 { 3 echo json_encode($data); 4 }

What about the RRT?

RRT is an optional part of the URL that allows the developer to reuse all the code and change only the rendering strategy.

For XML you can call: rrt=xml

For JSON you can call: rrt=cd (Code)

Conclusions:

This is not a real tutorial.. ok, i'm sorry! still I hope it can help you quick start!

    1. We need to create some files or place in an existing one

    2. 1 function in the controller

    3. 1 function in the model (or use an existing one)

    4. 1 function in the view

And you are ready to grow your application! Good luck!

* If you want to learn about data access please read data page or the first tutorial (DAO & VO Sections)