<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>P18X</title>
	<atom:link href="http://joomlatutorial.p18x.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://joomlatutorial.p18x.com</link>
	<description>Tutorial about Joomla component development</description>
	<lastBuildDate>Tue, 25 Aug 2009 15:32:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>joomla 1.6 native MVC component tutorial 2: Component structure</title>
		<link>http://joomlatutorial.p18x.com/joomla-1-6-native-mvc-component-tutorial-component-structure/</link>
		<comments>http://joomlatutorial.p18x.com/joomla-1-6-native-mvc-component-tutorial-component-structure/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 08:05:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog tutorial]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[Joomla]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[native component]]></category>
		<category><![CDATA[structure]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://p18x.co.cc/blog/?p=17</guid>
		<description><![CDATA[Lets go even further with our component and use the MVC concept. The controller will load the view and bind the model, the view will call a model function the will return the information, after that will pass the information to the layout to be displayed


Part I: Component entry point
As i said before, Joomla framework [...]]]></description>
			<content:encoded><![CDATA[<p>Lets go even further with our component and use the <abbr title="Model-View-Controller"><a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC</a></abbr> concept. The controller will load the view and bind the model, the view will call a model function the will return the information, after that will pass the information to the layout to be displayed<br />
<span id="more-17"></span></p>
<div class="p18x_post p18x_tutorial">
<h2>Part I: Component entry point</h2>
<p>As i said before, <a href="http://joomla.org">Joomla</a> framework will access the component through one single file. This file needs to have the same name as the component (not as the component's folder). A demo for this step was done the the previous post, and we'll take it from there, so it's a good idea to go and download the hole zip file as we'll work on that</p>
<p>Ok, we see that our component works so far. Let's delete the contents of the entry file <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn13').innerHTML; document.getElementById('syn13').innerHTML=t;return false" class="tooltip" href="#syn13">{joomla_server_path}+/components/com_+{component_name}+/+{component_name}+.php</a></span><span style="display:none" id="syn13"><strong>/joomla/server/path</strong>/components/com_<strong><strong>blog_tutorial</strong></strong>/<strong>blog_tutorial</strong>.php</span> and put this
</p>
<pre class="brush: php">

//this will load our base controller
jimport(&quot;joomla.application.component.controller&quot;);
//get the instance of our controller
$controller = &amp;JController::getInstance(&quot;Blog_tutorial&quot;);
//execute the controller
$controller-&gt;execute(JRequest::getVar(&quot;task&quot;));
$controller-&gt;redirect();
</pre>
<p>
Typically this is all there's needed in the component's main file. It basically loads the controller and executes the specified task or the default task ('display') if none is specified, then redirects if redirect instruction is set.
</p>
<h2>Part II: The controller</h2>
<p>
You can say that the controller is the center of a tipical Joomla component because every request to that component passes through the controller.
</p>
<p>
The controller can be used in two ways, when getting or when posting information</p>
<ul>
<li>
		A typical GET request to the component specifies, besides the component name (option=com_blog_tutorial), the view desired. In this case, the default task represented by the display() function is called
	</li>
<li>
		A typical POST request to the component specifies, besides the component (option=com_blog_tutorial), the task. In this case, the component entry point sets the task from the controller to be executed represented by a function with the same name. This function does not need views, because it just does something on the server and redirects to a proper GET flow, it will however load, if necessary, models to update the data, or in simpler cases will operate directly to the database.
	</li>
</ul>
<p>
	For now we'll just concentrate on GET requests, meaning that we'll end up in the 'display()' function. But before this, let's create our controller.
</p>
<p>
	The main controller resides in a file called 'controller.php' inside the component folder, here's the syntax: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn14').innerHTML; document.getElementById('syn14').innerHTML=t;return false" class="tooltip" href="#syn14">{joomla_server_path}+/components/com_+{component_name}+/controller.php</a></span><span style="display:none" id="syn14"><strong>/joomla/server/path</strong>/components/com_<strong><strong>blog_tutorial</strong></strong>/controller.php</span>. The class name has this syntax <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn15').innerHTML; document.getElementById('syn15').innerHTML=t;return false" class="tooltip" href="#syn15">{Component_name}+Controller</a></span><span style="display:none" id="syn15"><strong>Blog_tutorial</strong>Controller</span>. Inside the controller's class let's ad a function called 'display()'. This function has the purpose of loading the view specified in the URL, or a default one, it will load the proper model (in most cases the one with the same name as the view) and attach it to the view. Also it will set the default layout for the view. Other things can happen here, but we'll get into more details in a future lesson.
</p>
<p>
Here is the full code of our <abbr title="web log">blog</abbr>'s controller found here: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn16').innerHTML; document.getElementById('syn16').innerHTML=t;return false" class="tooltip" href="#syn16">{joomla_server_path}+/components/com_+{component_name}+/controller.php</a></span><span style="display:none" id="syn16"><strong>/joomla/server/path</strong>/components/com_<strong><strong>blog_tutorial</strong></strong>/controller.php</span>:
</p>
<pre class="brush:php">

&lt;?php
defined('_JEXEC') or die('DIRECT ACCESS NOT ALLOWED');
//import the base controller file
jimport('joomla.application.component.controller');

class Blog_tutorialController extends JController
{
	function display()
	{
		//get a reference to the 'document' object
		$document	= &amp;JFactory::getDocument();

		//get the view, type, and layout
		$vName		= JRequest::getWord('view', 'posts');
		$vFormat	= $document-&gt;getType();
		$lName		= JRequest::getWord('layout', 'default');
		//load the view
		if ($view = &amp;$this-&gt;getView($vName, $vFormat))
		{
			//load the model
			$model	= &amp;$this-&gt;getModel($vName);
			//attach the model and the layout to the view
			$view-&gt;setModel($model, true);
			$view-&gt;setLayout($lName);

			$view-&gt;assignRef('document', $document);
			//display the component
			$view-&gt;display();
		}
	}
}
?&gt;
</pre>
<h2>Part III: the model</h2>
<p>
Models inside Joomla handle getting and modifying entities inside our component. As we know every blog has posts or comments to those posts, well they are represented by models, a model for posts, and a model for comments. This is for the beginning, every individual post is again represented by a model, the post model, and so on, for what ever entity we can consider inside our blog component.
</p>
<p>
Of course we'll come across situations where we need information from the post models and from the comments module on a single request. This situation can be handle in tree different ways:
</p>
<ul>
<li>we'll consider the comments as being the same entity as the post, something that the post will always have. this is suitable for simple situations, if you'll have a complex comments system this is not the best choice</li>
<li><span class="note small">(I'm not really sure about this)</span> we'll bind to the view the post model, and let him load the comments model, and when the view asks the model for some comments, the post model will ask the comments model for the comments</li>
<li>we could bind to the post view, besides the post model, the comments model, so the view can access it, and ask for the comments </li>
</ul>
<div class="note important">
<p>I'm not sure what is the best solution, at first we'll use the first situation and until we get in detail with models, I'll have a better advice for all this</p>
</div>
<p>
Now lets get to where things go. The models go in the 'models' folder inside the component folder: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn17').innerHTML; document.getElementById('syn17').innerHTML=t;return false" class="tooltip" href="#syn17">{joomla_server_path}+/components/com_+{component_name}+/models</a></span><span style="display:none" id="syn17"><strong>/joomla/server/path</strong>/components/com_<strong><strong>blog_tutorial</strong></strong>/models</span> and it's file must be named the same as the model. Every model must have the following name as the class name, this is the example for the 'posts' model <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn18').innerHTML; document.getElementById('syn18').innerHTML=t;return false" class="tooltip" href="#syn18">{Component_name}+Model+{Model_name}</a></span><span style="display:none" id="syn18"><strong>Blog_tutorial</strong>Model<strong>Posts</strong></span></p>
<h3>The model class structure</h3>
<p>First thing we need to do is to include the base controller class from witch we'll extend our controller, this is done with he 'jimport()' function</p>
<pre class="brush:php">

jimport('joomla.application.component.model');
</pre>
<p>We already know the class name from above that will extend 'JModel' base class. We'll need a variable (member) inside this class that will hold our actual data, we'll call this member 'posts' or '$_posts'. This member will be protected, because we don't need direct access to it, it will be fetched to us by a function inside this class (public method)</p>
<pre class="brush:php">

class Blog_tutorialModelPosts extends JModel
{
	protected $_posts = null;
}
</pre>
<p>All we need now is the method to get the posts out of the model, we'll call our function 'getPosts()' and will return a reference to our '$_posts' member. It's a good practice to name our 'get' methods descriptively, meaning that if we what to get the posts, we'll name our method 'getPosts', in this case we allow Joomla facilitate easy access to out model data, but we'll talk about this particular thing in detail in a future chapter. For now, the model will return some 'hard coded' information because we didn't get to the database operations part yet. And here's the function:</p>
<pre class="brush: php">

	public function &amp;getPosts()
	{
		//if this is the first time getting the posts
		if(!$this-&gt;_posts)
		{
			$posts = array();
			$posts[] = 'First post.';
			$posts[] = 'Last post';
			$this-&gt;_posts = $posts;
		}
		//return the posts
		return $this-&gt;_posts;
	}
</pre>
<h2>Part IV: the view</h2>
<p>What does the view do? I probably mention this already before, but we'll talk here i a little more detail here. The view gets the information needed to be displayed, after it has the information, any modification needed is done here, as an example it can calculate the days since the post has been created based on the creation date. After the modifications took place, the view assigns the information needed to be displayed to the layout file</p>
<h3>The view file</h3>
<p>
The view file is simply called <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn19').innerHTML; document.getElementById('syn19').innerHTML=t;return false" class="tooltip" href="#syn19">view+{view_type}+.php</a></span><span style="display:none" id="syn19">view<strong>.html</strong>.php</span> no matter what the view itself is called, the only difference between views is the folder they reside in. Each view must be placed in a folder with the same name as the view, all these folders are located inside the 'views' folder inside the component folder. Here's the path of the 'posts' view for our blog: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn20').innerHTML; document.getElementById('syn20').innerHTML=t;return false" class="tooltip" href="#syn20">{joomla_server_path}+/components/com_+{component_name}+/views/+{view_name}+/view+{view_type}+.php</a></span><span style="display:none" id="syn20"><strong>/joomla/server/path</strong>/components/com_<strong><strong>blog_tutorial</strong></strong>/views/<strong>posts</strong>/view<strong>.html</strong>.php</span>.
</p>
<h3>The view class</h3>
<p>
The view class for you posts needs to have the following structure: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn21').innerHTML; document.getElementById('syn21').innerHTML=t;return false" class="tooltip" href="#syn21">{Component_name}+View+{View_name}</a></span><span style="display:none" id="syn21"><strong>Blog_tutorial</strong>View<strong>Views</strong></span>. Now... let's get to the code, all this class needs to do for now is to get the data from the controller assigned to this view, and pass it to the model.
</p>
<p>
Include the Joomla base view class from witch we'll extend our view
</p>
<pre class="brush:php">

jimport('joomla.application.component.view');
</pre>
<p>Get our posts from the model, the default model that is, because we can have multiple models to a view, but we'll talk about this later. Notice that we only have to access the 'get()' method, and the view automatically calls the get+{first_parameter} on the model</p>
<pre class="brush:php">

$posts = &amp;$this-&gt;get('Posts');
</pre>
<p>Now all we need is to assign the information to the layout and display it</p>
<pre class="brush:php">

	$this-&gt;assignRef('posts', $posts);
	parent::display($tpl);
</pre>
<p>And here's the full code for the view file</p>
<pre class="brush:php">

defined('_JEXEC') or die('Restricted Access');
jimport('joomla.application.component.view');

class Blog_tutorialViewPosts extends JView
{

	public function display($tpl=null)
	{
		//get the posts
		$posts = &amp;$this-&gt;get('Posts');
		//assing the posts to the view
		$this-&gt;assignRef('posts', $posts);
		//display
		parent::display($tpl);
	}
}
</pre>
<h2>Part V: the layout</h2>
<p>And finally the layout, so we can display the information. The layout needs to be here: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn22').innerHTML; document.getElementById('syn22').innerHTML=t;return false" class="tooltip" href="#syn22">{joomla_server_path}+/components/com_+{component_name}+/views/+{view_name}+/tmpl/+{layout_name}+.php</a></span><span style="display:none" id="syn22"><strong>/joomla/server/path</strong>/components/com_<strong><strong>blog_tutorial</strong></strong>/views/<strong>posts</strong>/tmpl/<strong>default</strong>.php</span>. The layout is just a HTML block that will present our information to the user's browser, accessing the data assigned to it is done with the '$this' keyword because the layout is part of the view class, and when assigning data to the layout we actually create/modify members of the view class. Here an example of a layout that will output a summary of our posts</p>
<pre class="brush: php; html-script: true">

&lt;?php defined('_JEXEC') or die('DIRECT ACCESS NOT ALLOWED')?&gt;
&lt;h2&gt;This blog's posts&lt;/h2&gt;
&lt;?php
if(count($this-&gt;posts))
{
	?&gt;
	&lt;ol&gt;
	&lt;?php
	foreach($this-&gt;posts as $p)
	{
		?&gt;
		&lt;li&gt;&lt;?php echo $p?&gt;&lt;/li&gt;
		&lt;?php
	}
	?&gt;
	&lt;/ol&gt;
	&lt;?php
}
else
{
	echo 'No posts yet';
}
?&gt;
</pre>
<h2>Conclusion</h2>
<p>That's about it, we now know where views and models are located, what they suppose to do and we have a few examples so we can build on them our future blog component. In our next tutorial we'll talk about database access and we'll add another view to our component to display a single component</p>
<p>Here's the full structure (don't forget to include a blank 'index.html' inside every folder you create), a screenshot and demo.</p>
<div id="files" class="jtree">
<ul>
<li class="open"><a href="#">{joomla_server_path}/components/</a>
<ul>
<li class="open"><a href="#">com_blog</a>
<ul>
<li><a href="#">models</a>
<ul>
<li>posts.php</li>
</ul>
</li>
<li><a href="#">views</a>
<ul>
<li><a href="#">posts</a>
<ul>
<li><a href="#">tmpl</a>
<ul>
<li>default.php</li>
</ul>
</li>
<li>view.html.php</li>
</ul>
</li>
</ul>
</li>
<li>blog_tutorial.php</li>
<li>controller.php</li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="clear"></div>
</div>
<div class="download_link">
	<a href="http://j16svn.p18x.com/index.php?option=com_blog_tutorial_2&#038;file=blog_tutorial" onclick="return hs.htmlExpand(this, { objectType: 'iframe' } )">Demo</a>&nbsp;<a href="http://joomlatutorial.p18x.com/data/files/joomla/blog_tutorial/2_structure/png/5_structure_1_P18X.png" onclick="return hs.expand(this)" >Screenshot</a>&nbsp;<a href="http://joomlatutorial.p18x.com/data/getfile.php?file=2_structure%2Fzips%2F5_structure_final_1_P18X" >Download ZIP file</a>
</div>
<p><script type="text/javascript">
jQuery(function () {
	jQuery("#files").tree({
		ui:{
			context: []
		}
	});
});
</script></p>
<div class="tutorial_intro">
<p>This tutorial will help you understand proper techniques for developing native MVC components within Joomla 1.6 environment.
</p>
<p>
At the time i started this tutorial, Joomla 1.6 was in the alpha state, the development and testing was done with the latest development version from the SVN. I will keep an eye on the development, so this tutorial will be up to date for any changes that will occur with Joomla system.
</p>
<p>
If you find mistakes, typos, security holes please let me know by posting a comment or by <a href="mailto:princo18x@gmail.com">mailing</a> me.
</p>
<p>
Through this tutorial we'll use the following conventions:</p>
<ul>
<li>Your site name: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn23').innerHTML; document.getElementById('syn23').innerHTML=t;return false" class="tooltip" href="#syn23">{site_name}</a></span><span style="display:none" id="syn23"><strong>http://example.com</strong></span></li>
<li>Your component name: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn24').innerHTML; document.getElementById('syn24').innerHTML=t;return false" class="tooltip" href="#syn24">{component_name}</a></span><span style="display:none" id="syn24"><strong>blog_tutorial</strong></span></li>
</ul>
<p style="color:red; font-weight:bold">
This tutorial was made by studying the core and the components of the Joomla CMS and from experience with developing joomla 1.5 components. I haven't used any copyrighted material, or materials from other tutorials. This tutorial represents just my point of view regarding Joomla development. Also, i am not affiliated in any way with <a href="http://joomla.org">Joomla</a> or <a href="http://www.opensourcematters.org/">Open Source Matters</a>, and this tutorial can not be considered an official one.
</p>
<p style="color:red; font-weight:bold">
This tutorial and the components resulting from it are offered free, and will always be free in some form, you can use them in any way you like without having to ask for permission, but things like: link back, keeping the author and author site (P18X, P18X.com) where they should be, mail or comment with impressions are always welcomed
</p>
</div>
</div>
<p><script type="text/javascript">
jQuery("a.tooltip").tooltip({ 
    bodyHandler: function() { 
        return jQuery(jQuery(this).attr("href")).html(); 
    }, 
    showURL: false 
});
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://joomlatutorial.p18x.com/joomla-1-6-native-mvc-component-tutorial-component-structure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Joomla 1.6 native MVC blog component tutorial 1:  the basics</title>
		<link>http://joomlatutorial.p18x.com/joomla-1-6-native-component-tutorial-basics/</link>
		<comments>http://joomlatutorial.p18x.com/joomla-1-6-native-component-tutorial-basics/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 08:43:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog tutorial]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Joomla]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[native component]]></category>

		<guid isPermaLink="false">http://p18x.co.cc/blog/?p=3</guid>
		<description><![CDATA[This is the first part of a series of tutorials about Joomla 1.6 native MVC component development. In this part we'll go over the basic stuff: directory structure, what files go where, what they represent. Also we'll talk about some theoretical stuff about what exactly MVC is in Joomla. I hope you'll enjoy it.




Part I: [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first part of a series of tutorials about <a href="http://www.joomla.org/">Joomla</a> <a href="http://joomlacode.org/gf/project/joomla/frs/?action=FrsReleaseBrowse&#038;frs_package_id=3585">1.6</a> native <abr title="Model View Controller"><a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC</a></abr> component development. In this part we'll go over the basic stuff: directory structure, what files go where, what they represent. Also we'll talk about some theoretical stuff about what exactly MVC is in Joomla. I hope you'll enjoy it.<br />
<span id="more-3"></span><br />
<!--
<div style="background:none" class="comm_date"><script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div>
<p>--><br />
<!--
<div id="ttest" style="border:1px solid red; background-color:white; position:fixed; right:50%; ">asd
</div>
<p>--></p>
<div class="p18x_post p18x_tutorial">
<h2>Part I: intro to Joomla components</h2>
<p>
	Every Joomla component is divided in two parts, the admin part or the back-end and the front-end part. The files for your component reside in their own folder called <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn36').innerHTML; document.getElementById('syn36').innerHTML=t;return false" class="tooltip" href="#syn36">com_+{component_name}</a></span><span style="display:none" id="syn36">com_<strong><strong>blog_tutorial</strong></strong></span>. The admin part and the front-end part are independent of each other, so we can publish components only for the admin or only for the front-end user, but usually every component has both parts present. For the front-end part, the component folder must be inside the components folder inside your joomla installation: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn37').innerHTML; document.getElementById('syn37').innerHTML=t;return false" class="tooltip" href="#syn37">{joomla_server_path}+/components</a></span><span style="display:none" id="syn37"><strong>/joomla/server/path</strong>/components</span>, and for the admin, inside the administrator components folder: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn38').innerHTML; document.getElementById('syn38').innerHTML=t;return false" class="tooltip" href="#syn38">{joomla_server_path}+/administrator/components</a></span><span style="display:none" id="syn38"><strong>/joomla/server/path</strong>/administrator/components</span>.
</p>
<p>
	The models and the views have their own separate folders called <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn39').innerHTML; document.getElementById('syn39').innerHTML=t;return false" class="tooltip" href="#syn39">{joomla_server_path}+/components/com_+{component_name}+/views</a></span><span style="display:none" id="syn39"><strong>/joomla/server/path</strong>/components/com_<strong><strong>blog_tutorial</strong></strong>/views</span> or <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn40').innerHTML; document.getElementById('syn40').innerHTML=t;return false" class="tooltip" href="#syn40">{joomla_server_path}+/administrator/components/com_+{component_name}+/views</a></span><span style="display:none" id="syn40"><strong>/joomla/server/path</strong>/administrator/components/com_<strong><strong>blog_tutorial</strong></strong>/views</span> and <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn41').innerHTML; document.getElementById('syn41').innerHTML=t;return false" class="tooltip" href="#syn41">{joomla_server_path}+/components/com_+{component_name}+/models</a></span><span style="display:none" id="syn41"><strong>/joomla/server/path</strong>/components/com_<strong><strong>blog_tutorial</strong></strong>/models</span> or <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn42').innerHTML; document.getElementById('syn42').innerHTML=t;return false" class="tooltip" href="#syn42">{joomla_server_path}+/administrator/components/com_+{component_name}/models</a></span><span style="display:none" id="syn42"><strong>/joomla/server/path</strong>/administrator/components/com_<strong><strong>blog_tutorial</strong></strong>/models</span>.
</p>
<p>
Each part of every component has only one entry point represented by a PHP file with the same name of the component.
</p>
<p>
	Accessing our component is done by adding <span class="tutorial_syntax">index.php?option=com_blog_tutorial</span> after your Joomla installation home page's URL
</p>
<p>
	Let's create our entry file for the front-end and just output a string, just for test
</p>
<pre class="brush:php">

&lt;?php
	echo 'Hello from Joomla blog tutorial component';
?&gt;
</pre>
<p>
If the hole thing about the structure wasn't clear enough, here a representation of the file system and a download link
</p>
<div id="files_1" class="jtree">
<ul>
<li class="open">
			<a href="#">{joomla_server_path}</a></p>
<ul>
<li class="open">
					<a href="#">administrator</a></p>
<ul>
<li>
							<a href="#">components</a></p>
<ul>
<li>
									<a href="#">com_blog_tutorial</a></p>
<ul>
<li>
											<a href="#">models</a>
										</li>
<li>
											<a href="#">views</a>
										</li>
<li>
											blog_tutorial.php
										</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
					<a href="#">components</a></p>
<ul>
<li>
							<a href="#">com_blog_tutorial</a></p>
<ul>
<li>
									<a href="#">models</a>
								</li>
<li>
									<a href="#">views</a>
								</li>
<li>
									blog_tutorial.php
								</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="clear"></div>
</div>
<div class="download_link">
	<a href="http://j16svn.p18x.com/index.php?option=com_blog_tutorial_1&#038;file=blog_tutorial" onclick="return hs.htmlExpand(this, { objectType: 'iframe' } )">Demo</a>&nbsp;<a href="http://joomlatutorial.p18x.com/data/files/joomla/blog_tutorial/1_basics/png/intro_to_joomla_components_1_P18X.png" onclick="return hs.expand(this)" >Screenshot</a>&nbsp;<a href="http://joomlatutorial.p18x.com/data/getfile.php?file=1_basics%2Fzips%2Fintro_to_joomla_components_P18X" >Download ZIP file</a>
</div>
<p><script type="text/javascript">
jQuery(function () {
	jQuery("#files_1").tree({
		ui:{
			context: []
		}
	});
});
</script></p>
<h2>Part II: intro to MVC</h2>
<p>
MVC stands for Model-View-Controller. I won't talk much about the general concept of MVC exept for what is specific to Joomla.
</p>
<h3>Models inside Joomla</h3>
<p>
	Models deal with retrieving information from the database and passing it to the view. Topically, models are loaded and assigned to views by the controller, one model to one view, but in certain circumstances the view can use more than one model. A model can use other models in the process of retrieving the information
</p>
<p>
	Models are located in a single folder called 'models', inside the component folder. Each model file name is the model's name. The model's class name must have the following structure (let's say that your model is called 'posts'): <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn43').innerHTML; document.getElementById('syn43').innerHTML=t;return false" class="tooltip" href="#syn43">{Component_name}+Model+{Model_name}</a></span><span style="display:none" id="syn43"><strong>Blog_tutorial</strong>Model<strong>Posts</strong></span></p>
<h3>Views inside Joomla</h3>
<p>
	The views for a component are located each in their own folder with the same name, all of that folder inside the views folder of your component. The file names for the views must follow this rule: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn44').innerHTML; document.getElementById('syn44').innerHTML=t;return false" class="tooltip" href="#syn44">view+{view_type}+.php</a></span><span style="display:none" id="syn44">view<strong>.html</strong>.php</span>. In this example our view is called 'posts' and the view type is 'html', the type of the view can vary, it can be for example 'rss', 'pdf', it basically describes the content.
</p>
<p> the role of the views is to retrieve information from the modules, manipulate the information if necessary and than pass it to the 'layouts' as i call them, to display them.</p>
<h4>Layouts</h4>
<p class="first">
Let's say a few words about layouts in Joomla.
</p>
<p>
Layouts actually display the information, they wrap the HTML tags arrond our posts, let's say, so they can be integrated into an unordered list for example. Ideally they contain only HTML markup and basic control structures, there are examples out \there of functions declared inside layouts, not good, if you have repetitive tasks to do in your layouts, use helpers, but that's another lesson
</p>
<p>
Layouts reside in the 'tmpl' folder inside the views folders to whom they belong.
</p>
<h3>Controllers</h3>
<p>
And finally, the controllers. The role of the controller is to make the connection between the models and the views, this is the typical situation of a GET request. On a POST request, the controller takes over some of the view's roles, meaning that the controller will control the models for, let's say, deleting a comment. There are examples out there that in the case of a POST request, the controller accessed the database to delete things.
</p>
<p><!--outro--></p>
<div class="tutorial_intro">
<p>This tutorial will help you understand proper techniques for developing native MVC components within Joomla 1.6 environment.
</p>
<p>
At the time i started this tutorial, Joomla 1.6 was in the alpha state, the development and testing was done with the latest development version from the SVN. I will keep an eye on the development, so this tutorial will be up to date for any changes that will occur with Joomla system.
</p>
<p>
If you find mistakes, typos, security holes please let me know by posting a comment or by <a href="mailto:princo18x@gmail.com">mailing</a> me.
</p>
<p>
Through this tutorial we'll use the following conventions:</p>
<ul>
<li>Your site name: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn45').innerHTML; document.getElementById('syn45').innerHTML=t;return false" class="tooltip" href="#syn45">{site_name}</a></span><span style="display:none" id="syn45"><strong>http://example.com</strong></span></li>
<li>Your component name: <span class="tutorial_syntax"><a onclick="var t = this.innerHTML; this.innerHTML=document.getElementById('syn46').innerHTML; document.getElementById('syn46').innerHTML=t;return false" class="tooltip" href="#syn46">{component_name}</a></span><span style="display:none" id="syn46"><strong>blog_tutorial</strong></span></li>
</ul>
<p style="color:red; font-weight:bold">
This tutorial was made by studying the core and the components of the Joomla CMS and from experience with developing joomla 1.5 components. I haven't used any copyrighted material, or materials from other tutorials. This tutorial represents just my point of view regarding Joomla development. Also, i am not affiliated in any way with <a href="http://joomla.org">Joomla</a> or <a href="http://www.opensourcematters.org/">Open Source Matters</a>, and this tutorial can not be considered an official one.
</p>
<p style="color:red; font-weight:bold">
This tutorial and the components resulting from it are offered free, and will always be free in some form, you can use them in any way you like without having to ask for permission, but things like: link back, keeping the author and author site (P18X, P18X.com) where they should be, mail or comment with impressions are always welcomed
</p>
</div>
</div>
<p><script type="text/javascript">
jQuery("a.tooltip").tooltip({ 
    bodyHandler: function() { 
        return jQuery(jQuery(this).attr("href")).html(); 
    }, 
    showURL: false 
});
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://joomlatutorial.p18x.com/joomla-1-6-native-component-tutorial-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

