We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

PhalconPhp really DRY ?

Hi :)

I'm really new with phalconPHP, you made a really great job, but i get a tiny question, Phalcon is it really DRY ?

I'm writing an web application (Snippet share system for phalcon community) and i'm already tired ...

I made a controller with a simple method :

<?php

class SnippetController extends ControllerBase
{
    public function addAction()
    {
        if ($this->request->isPost())
        {
            // Get post values
            $language = $this->request->getPost('language');
            $description = $this->request->getPost('description');
            $code = $this->request->getPost('code');

            $snippet = new Snippets();
            $snippet->description = $description;
            $saved = $snippet->save();

            if ( ! $saved) 
            {
                $errors = '';

                foreach ($snippet->getMessages() as $message) {

                    $errors .= $message->getMessage() . '<br/>';

                }

                $this->flashSession->error($errors);
            }

        }

        // Set languages code into the view
        $this->view->setVar('languages', $this->config->languages->toArray());

    }
}

And a simple model :

<?php

use Phalcon\Mvc\Model\Validator\PresenceOf;

class Snippets extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $description;

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'description' => 'description'
            );
    }

    public function validation()
    {
        $this->validate(new PresenceOf(

            array(

                'field' => 'description',
                'message' => 'Le champ description est requis'

                )
            ));

        $this->validate(new PresenceOf(

            array(

                'field' => 'code',
                'message' => 'Le champ code est requis'

                )

            ));

        if ($this->validationHasFailed() === TRUE) {
            return FALSE;
        }
    }

}

Just to check 2 tiny required fields, i wrote more than 16 lines (A system to get error messages, and $this->validate() system)

In my previous framework, i made this for example :


// Add rules
$this->validation->rules('field_to_check', 'message', 'required|max_length[2]|equal[another_field]');

// Get errors
$this->validation->errors(); // Ready to print in the view

Don't forget i'm really new with Phalcon, maybe i made wrong/worst code, so what's the DRY way to check forms ?

Thx ;)

edited Oct '14

I too am some what new to Phalcon, depending on what your definition of new is, and I tend to agree with you. I too use Laravel and love it for the reasons you outlined. There are so many things that Laravel just does for you! That's really cool and makes creating an app or API totally painless.

But, I chose to pick up Phalcon for a few reasons.

  1. The concept of Phalcon is way cool!

  2. Because of that concept and the hard working, stead fast team behind it, I believe in it. Not that I don't believe in Laraval. No no. Laravel will always be a bit large than Phalcon, but this is not a who has more users race. This is not a race at all. I will use Laravel again, but not before getting to know Phalcon a bit better first.

  3. Yes, I do have some issues with some of the syntax. But just bear in mind - Syntax is a very personal thing. It's ok to not like a framework because of the syntax. No one here will hunt you down. But, I will add this: If you were to use a micro-framework, such as Slim, Aura or Silex, you will find basically the same thing, unless you bring in a third party library that takes care of some of those tasks that tend to get a bit... codey.

  4. It's different than any other frameworks out there. I am a fairly new web developer (2.5 to 3 yrs). When using a framework like Laravel, you tend to learn more about the framework than the code. Using a micro-framework or something that is a bit different, like Phalcon, I am forced to look at things differently and research a lot. In essence, I am forcing myself to learn more by doing more. I could have chose Lithium and got the same result. Lithium is different, but not as cool as Phalcon.

Just my opinion.

"Somebody" ;-) have to write just a Class to extend a Model with your validation "rules()" Function. I really like on Phalcon that it give you the ability todo such things and not restrict everything with predefined classes.

I think you're misusing the term DRY (which means Don't Repeat Yourself). The code you wrote is DRY in that your code doesn't do the same thing in multiple places.

As far as lines of code go - you're right, your previous framework's code is much shorter. However, it would appear that Phalcon's is more robust and extensible - at least in this particular example. I think generally Phalcon tries to be a toolkit, whereas other frameworks (perhaps Laraval) try to do all the work for you. Personally I prefer using a toolkit, because invariably when using a framework you eventually come across a situation the framework isn't built for, and you have to jump through a million hoops to get it to work.