Tutorial: Controllers and Actions in LightVC

August 25, 2009
Tags: ,

In this tutorial, we continue to create an MVC application using the bare LightVC and CoughPHP distributions, focusing on Controllers and Views in LighVC.

For this tutorial, you will need to have the skeleton application created in the previous article on combining CoughPHP and LightVC, as well as our database table and generated CoughPHP code.

Step 1: Creating a new controller and action in LightVC.

Create a new file in controllers/ called post.php:

<?php
 
class PostController extends AppController
{
	public function actionIndex()
	{	
 
	}
 
}
 
?>

This creates a new controller, page and a the default index action.

Now we need to create a view corresponding to the action.
Create a folder in the views/ folder called post.
Create a file in views/post/ called index.php:

<?php echo time() ?>

(Notice how the name for the folder corresponds to the lowercase name of the controller, and the name of the view corresponds to the lowercase name of the action)

Now if you’ve been following along the tutorial, navigate to http://cms.rhp.org/post/ and you should see the result of your work:

New Action

New Action

This page is composited from several different parts.
The outer shell comes from views/layouts/default.php
The inner content comes from the view we just edited views/post/index.php
The css resides in webroot/css/master.css

Let’s edit the layout to remove the congratulations messages:

<body>
 
	<div id="header">
		Welcome to cms.rhp.org
	</div>
 
	<div id="content">
		<?php echo $layoutContent ?>
	</div>
 
</body>

Reload the page, and see the changes.

Now let’s put that action we created to some use by listing all the posts we’ve got in the system.

Step 2: Listing Posts

The first thing we need to do is create a method on Post_Collection to load all the non-deleted posts:

public function loadActive()
{
	$sql = '
		SELECT
			*
		FROM
			post
		WHERE
			is_deleted = 0
		ORDER BY
			post.name
	';
 
	$this->loadBySql($sql);
 
}

This is pretty straightforward, some SQL to fetch all the posts which we then pass to the loadBySql method that Post_Collection inherits from CoughCollection. This will populate the collection with all the posts. Note that the order in which the rows are retrieved will be the order they are in in the collection (unless we sort the collection after the fact), so we set an ORDER BY in the SQL statement.

Now to use it:

Edit the actionIndex method to create an instance of Post_Collection, and populate it, and then pass it to the view:

public function actionIndex()
{	
	$posts = new Post_Collection();
	$posts->loadActive();
 
	$this->setVar('posts', $posts);
}

Again, a note on conventions the same way we’re going to be using the singular for table names and objects representing individual rows in the table, we’re going to use the plural for aggregates.

Now edit views/post/index.php:

<ul>
	<?php
	foreach ($posts as $post)
	{
		?>
		<li>
			<?php echo htmlentities($post->getName()) ?>
		</li>
		<?php
	}
	?>
</ul>

Note that we call htmlentities on display to prevent any bad data in the db from messing up our page display.

Reload your index page, and if you’ve been following the tutorial, you should see the following:

Finished Page

Congratulations, you are dynamically reading data out of the db and displaying it on your page!

Next time: viewing and editing posts! Creating new posts!

5 Responses to “Tutorial: Controllers and Actions in LightVC”

  1. Keep spreading the gospel of CoughPHP/LightVC and MVC design patterns in general! I can’t wait for you guys to get up into the more advanced subjects so I can continue /re?aping/ your brains 😀

  2. @Richard Lindsey
    Absolutely, I’ve got all sorts of crazy ideas in the pipeline, even some stuff I haven’t even tried to implement yet…

  3. Thanks for the article!

    Regarding: “Notice how the name for the folder corresponds to the lowercase name of the controller, and the name of the view corresponds to the lowercase name of the action”

    Controller and action names are not actually lower case variants of the class name / method name. The class name and action name are the title cased version of the name used in the URL. Using a multi-word controller/action name (with spaces replaced with underscore) helps demonstrate this: http://lightvc.org/docs/quickstart_guide/

  4. pretty exciting so far, when are we going to have the next article on viewing, editing and creating posts…. doing a complete application will help me fully understand the work flow and advantages of using MVC.

  5. i think it doesnt work becoz i did not change the directory permissions with chmod coz i am using windows. i could use attrib but i couldnt figure out exactly what you were trying to do.