Yii2 versus symfony 4 – a framework comparison

I have long worked with Yii and the symfony framework and want to try the following to explain the differences between the two frameworks from programmers point of view.

Configuration of environments

When you create Web applications is typically at least 2 Environments: The development environment, on which you want to see errors and the live environment, where this should happen. To come test environment to run automated tests and perhaps a staging environment.

Symfony raises the very skillfully, with the .env files and own folders within the configuration folder.

At Yii must the programmers themselves lend a hand, to load the correct configuration, There are only from home 3 (dev, Prod, test) Vorgsehen environments, then it is confusing. more…

Yi custom CButtonColumn in Gridview

This button has an own image of bootstrap by using the class icon-download and will open in a new tab.

   array(
                'class' => 'zii.widgets.grid.CButtonColumn',
                'template'=>'{pdf}',
                'buttons'=>array(
                    'pdf' => array(
                        'url' => 'Yii::app()->createUrl("/con/action",array("id"=>$data->id))', 
                        'options' => array('target' => '_blank', 'class' => 'icon-download'),
                        'label' => '',
                        'imageUrl' => false,
                    ),

Yii2 OpenStreetMap extension

I've developed îêñè simple OpenStreetMap extension for Yii2, with which you can show any points with a ToolTip. As a card provider OpenLayers used.

Insallationsanweisungen:

  1. Copy of the archive content/widgets/OpenLayers
  2. Einbinder extension in any view:
<?php
/* @var $this yii\web\View */

use app\widgets\OpenLayers\OpenLayers;
use yii\helpers\BaseVarDumper;
use yii\helpers\Html;
?>
<div>

    <div>
        <h1>Multiple Dots</h1>
        <h2>Input</h2>
        <?php $cities = [
            ["name" => "Belize City", "lng" => -88.1975600, "lat" => 17.4995200, "link" => Html::a("link", "http://www.routenplaner24.de/n/stadtplan/belize+city-3582677"), "population" => "3"],
            ["name" => "San Ignacio", "lng" => -89.0796400, "lat" => 17.1576000, "link" => Html::a("link", "http://www.routenplaner24.de/n/stadtplan/san+ignacio-3581194"), "population" => "5"],
            ["name" => "Orange Walk", "lng" => -88.5632800, "lat" => 18.0812400, "link" => Html::a("link", "http://www.routenplaner24.de/n/stadtplan/orange+walk-3581514"), "population" => "6"],
        ];
        BaseVarDumper::dump($cities, 10, true);

        ?>
        <h2>Map</h2>
        <?php echo OpenLayers::widget(['cities' => $cities, 'map_id' => 'multiple']) ?>

    </div>
</div>

and you're done…

yii2_open_street_map_extension

Yii2 OpenStreetMap extension / Widget

 

Download of the extension as .zip file

Yii for professionals: Book review by Yii project blueprints

The September 2014 published book “Yii project blueprints” Grabs from the publishing Publisher comes from a Reihge of good English-language books on the PHP framework-Yii. In the beginning, I was very skeptical, If I would keep the 43te HowTo about how to create a CMS or PM ticket tools in my hands, but I was very positively surprised:This book is aimed at experienced Yii programmer, who want to dive into the depths of the framework.

The first two chapters are rather intended for the entry, but then you meet amazingly interesting tricks with the framework. A few examples:

  • Integration of composer
  • many safety-related tips: Real random number generation, Markdown-parser, etc.
  • Cronjobs with Yiic
  • Focus on reusability and independence of the components, e.g.. explains exactly, How to use modules and how to best to reuse this
  • Flash messages
  • Use an email-Sende and receive service on the BSP. by SendGrid
  • Authentication of users with HybridAuth: Google , Twitter, Facebook etc. Accounts
  • SEO: Slug URLs and implementation of Sitemap/RSS feeds
  • Implementation of a RESTful API

The only downside are the rare screenshots, you have can contribute much to the understanding of the often very long code examples.

Yii Griedview ID not defined

If you use the Yii GridView, to pack quickly when dataProvider data in a table, you have to watch, that one defines, What is the unique identifier for each row. In a CArrayDataProvider, you must have the attribute”keyField” set :

$this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => new CArrayDataProvider($myData, array("keyField" => "myKeyFieldName")),
        )
    ));
    ?>

otherwise, there is an error message:

xxx.id nicht definiert

You have to watch, which provider to use, at one CActiveDataProvider is the attribute different then again – Attention!:&nbsp;”keyAttribute”

$this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => new CActiveDataProvider($myActiveRecords, array("keyAttribute" => "myKeyFieldName")),
        )
    ));
    ?>

Pass Yii PHP objects into JavaScript

There are different variations to pass the PHP variables and objects to JavaScript:

1. with PHP json_encode()

var employmentTypesIds = <?php echo json_encode($employmentTypesIds)?>;

2. per extension with active record relations! – carjson

3. via script transfer:

$cs->registerScript('php_vars',
            'messages1 = '.CJSON::encode(Yii::app()->createAbsoluteUrl("/messages/hello")).';'.
            'messages1 = '.CJSON::encode(Yii::app()->createAbsoluteUrl("/messages/goodbye")).';'
        );

Enable Yii session handling at the database

For security reasons, you can the session with Yii does not have the file system, but the database manage can automatically encrypt the session variable.

Following configuration is required:

'components' => array(      
     'session'=>array(
            'class' => 'system.web.CDbHttpSession',
            'connectionID' => 'db',
            'sessionTableName' => 'session',
            'autoStart'=>true,
            'cookieMode'=>'only',
            //'savePath'=>'/tmp',
            'sessionName'=>'connected_session',
            'timeout'=>60*60,// 1 hour
            'useTransparentSessionId'=>false,
        ),

Yii CGridView custom header for PDF generation

To generate a PDF with mpdf, do you want to remove the thead tags, Since these not converted by mpdf. You can use the following class:

Yii::import('zii.widgets.grid.CGridView');

class PDFGridView extends CGridView {

    public function renderTableHeader() {
        $options['class'] = 'pdf-grid-header';
        echo CHtml::openTag('tr', $options) . "\n";
        foreach ($this->columns as $header) {
            $header->renderHeaderCell();
        }
        echo CHtml::closeTag('tr') . "\n";
    }
}

View:

 $this->widget('PDFGridView', array(...)