0xa7 VSCODE + SYMFONY WEB-SERVER

Задумался на тем, как бы мне не ходить ручками в терминал, чтобы запустить встроенный web-сервер Symfony, и не делать каждый раз вот это bin/console server:run 8081, при чем сначала нужно выйти в корень Symfony-проекта, что частенько просто бесит.

Continue reading →

symfony

 

0x90 FTN PACKET TOSSER - FIRST MOVEMENTS

I've decided to write ftn-tosser based on separated libraries:

  • FTNPacket
  • FTNArchive
  • etc...

First movements are packet parser and message serializer:

fidonet symfony php

 

0x7f SYMFONY CONSOLE, LEARN WHILE WRITING AREAFIX FOR IFMAIL NODE

Go to project dir and create composer.json

{
    "require": {
        "symfony/console": "^3.1",
        "symfony/finder": "^3.1",
        "psr/log": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "": "src/"
        }
    }
}

Then run install:

composer install

Create application script file and make it executable:

touch areafix
chmod a+x areafix

Content of areafix file:

#!/usr/bin/env php

<?php
// set to run indefinitely if needed
set_time_limit(0);

/* Optional. It’s better to do it in the php.ini file */
date_default_timezone_set('Europe/Kiev');.

// include the composer autoloader
require_once __DIR__ . '/vendor/autoload.php';.

// import the Symfony Console Application.
use Symfony\Component\Console\Application;.
use Commands\GreetCommand;

$app = new Application();
$app->add(new GreetCommand());
$app->run();
?>

I put my commands to src/Commands. Command example (from http://symfony.com/doc/current/components/console/introduction.html#creating-a-basic-command):

namespace Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GreetCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('demo:greet')
            ->setDescription('Greet someone')
            ->addArgument(
                'name',
                InputArgument::OPTIONAL,
                'Who do you want to greet?'
            )
            ->addOption(
               'yell',
               null,
               InputOption::VALUE_NONE,
               'If set, the task will yell in uppercase letters'
            )
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument('name');
        if ($name) {
            $text = 'Hello '.$name;
        } else {
            $text = 'Hello';
        }

        if ($input->getOption('yell')) {
            $text = strtoupper($text);
        }

        $output->writeln($text);
    }
}
symfony

 

0x74 SYMFONY - ENTITY VALIDATION

app/config/config.yml

validation:      { enabled: true, enable_annotations: false }

Bundle/Resources/config/validation.yml

Bundle\Entity\Post:
    properties:
        name:
            - NotBlank: ~
        text:
            - NotBlank: ~

Controller

$entity = new Entity();
$validator = $this->get('validator');
$errors = $validator->validate($entity);
if (count($errors)>0) {
} else {
}
symfony php

 

0x6a SYMFONY NOTIFICATION - FLASHBAG

Create notification in controller:

$this->addFlash(
    'notice',
    'Your changes were saved!'
);

$this->addFlash is equivalent to $this->get('session')->getFlashBag()->add

Twig template to show notification:

{% for flash_message in app.session.flashbag.get('notice') %}
<div class="alert alert-success">
    {{ flash_message }}
</div>
{% endfor %}
symfony

 

0x68 SYMFONY2 - CREATE BUNDLE

bin/console generate:bundle 

It will ask some questions and creates all minimal needed files.

symfony