Thinking of using Yii. Need some pre-advice :)

Hi guys,

I’m thinking of switching to Yii after 8 years of adodb (AR) + smarty + my own custom MVC and I’m having trouble grasping some concepts. First of all I don’t like the usage of OOP to generate form fields in Yii views and was wondering if it is possible not tu use them but still have the functionality needed?

For example this is the default code from Yii:


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'contact-form',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name'); ?>

		<?php echo $form->error($model,'name'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'email'); ?>

		<?php echo $form->textField($model,'email'); ?>

		<?php echo $form->error($model,'email'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'subject'); ?>

		<?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>128)); ?>

		<?php echo $form->error($model,'subject'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'body'); ?>

		<?php echo $form->textArea($model,'body',array('rows'=>6, 'cols'=>50)); ?>

		<?php echo $form->error($model,'body'); ?>

	</div>


	<?php if(CCaptcha::checkRequirements()): ?>

	<div class="row">

		<?php echo $form->labelEx($model,'verifyCode'); ?>

		<div>

		<?php $this->widget('CCaptcha'); ?>

		<?php echo $form->textField($model,'verifyCode'); ?>

		</div>

		<div class="hint">Please enter the letters as they are shown in the image above.

		<br/>Letters are not case-sensitive.</div>

		<?php echo $form->error($model,'verifyCode'); ?>

	</div>

	<?php endif; ?>


	<div class="row buttons">

		<?php echo CHtml::submitButton('Submit'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

and this to me is definitly NOT separation of logic and design. Give that “template” code to a designer in your company and see if he can change the design. Ain’t gonna happen. Also as a programmer you then have to “translate” his design to PHP code that will eventually again generate that same design!? Even 7 or 8 years back I was avoding the usage of HTML/form generating OOP classes in PHP. So what I’m wondering is - will the posted code work if I replace


<?php echo $form->textField($model,'name'); ?>

and


<?php echo $form->textArea($model,'body',array('rows'=>6, 'cols'=>50)); ?>

with actual HTML code? Would that change anything regarding Yii functionality? I’m ok with the form header being done by Yii (not thrilled but ok).

Second question. How to get Yii to understand SEO links in this formats:


www.domain.com/product/name_of_the_product.html ("product" is a static word)

or


www.domain.com/group_name/sub_group/sub_group_id

or


www.domain.com/contact_us.html

www.domain.com/about_us.html

with all being ofcourse dynamic PHP pages…

Third question. If i understood Yii than my Poll should be a widget right?What’s the difference between module and a widget?

To answer the second part of the question see this.

thanx…been reading the documentation for the last 2 days…

Good. That’s one question answered.

The CActiveForm makes a HTML Form. Duh. This HTML Form gives each field the correct name to allow massive assignment of variables. Labels are automatically generated, required fields gets a * and on-error the fields automatically become red.

An example of generated HTML could be:

<input type="text" name="User[name]" maxlength="45" />

<input type="password" name="User[password]" />

etc.

Basically you want to use the Widget.

If you don’t, you don’t have to. If you don’t follow the name=“ModelName[AttributeName]” convention you’d have to adapt the Controller to allow massive assignment, and you’d probably have to change some other logic here and there as well.


i dont neccesarily agree with you on template code, but I understand where you’re coming from. Remember that a template designer also has to learn a template language like Smarty. Now he has to learn how to use the Yii widgets. As you can see in your example, PHP is stricly used to deal with view-logic. You as a programmer do not have to translate his PHP design into HTML, I don’t really understand what you mean.

I have been using the Yii framework for a few weeks now, not long at all. Still have to learn a few things, but I enjoy programming in it :)

CakePHP is more strict in it’s conventions and rules. If you prefer a more stricter ruleset you could look into that framework as well.

Yii forces nothing upon you, you can choose how to do pretty much everything, even use ADODB rather than Active Records. Most of the CActiveForm helpers map back to HTML fairly simply but I know what you mean, I’ve had interface people who can’t/won’t some basic PHP, and make everyone’s life easier. At the end of the day though, if they can only do pure HTML, there will always be translation between the two. Of course they could just look at the markup and apply appropriate CSS to just make it look right and still use the active form rendering.

As for the difference between widgets and modules:

Widgets are used to render some data on a page and allow simple code reuse to display that rendering across multiple pages.

Modules are, pretty much, self contained sites. They contain their own controllers and views and allow you to split the site up into self-contained sections.

So for your poll you would probably have the rendering in a widget but the entering ect wrapped into a controller etc.

Yii, and all MVC framework may look confusing when you start using them, but soon you will see advantages of it(and especially your clients, bosses, as your development cycle will fasten).

I also had my own database files, libraries, but that was long time ago. WHy to reinvent wheel always(off course there are some cases where you might not need MVC frameworks, or frameworks at all, but that are rare cases, especially for small projects).

So, just go ahead, and get yourself familiar with Yii framework, you will not regret for time you spent learning it.

CPortlet is extended from CWidget. You can extend CPortlet to create the equivalent of a Poll block. Otherwise, you can add the widget code each time you want to use it. The portlet solution should minimize code in your views.

You might want to take a look at the Block extension from Jayrulez. It allows a layout or template to be divided into regions and those regions can contain collections of portlets (blocks). I have something similar in development but it is used in conjunction with a CMenu portlet that stores any number of menus in a database. The menu portlet allows collections of portlets to be associated with individual menu items. Thus, a Shop menu item would display a shop module along with shop-specific portlets, or a CMS menu item would display a CMS module with CMS-related portlets, and so on.

Note that the form code contains no CSS code. The CSS is associated with the HTML elements in the form and applied in the main layout or theme (template). In the case of themes, a designer can also skin them. You can create themes using PHP without a template engine or use one of the renderer classes developed by Yii or third parties (Prado, Smarty, Dwoo, Twig, etc.).

You might keep in mind that themes or a main layout can display numerous nested views. Yii refers to these as partials which are basically view fragments. Your form is a partial view that Yii embeds in a parent view which is embedded in a module layout and then embedded in a main layout or theme when loaded.

Your designer only needs to work with a theme or main layout (whatever your preference) and the associated CSS files. When used in conjunction with something like the block extension, you can minimize code in your theme or main layout significantly which is what you want.

The sudden change of platform after such a long time frame would require you to look into it for sometime. The experts who’ve posted above… would add insight.

/* moved - not a tip, snippet or tutorial */