PHP code example of gyro / mvc-bundle

1. Go to this page and download the library: Download gyro/mvc-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gyro / mvc-bundle example snippets


$bundles = [
    // ...
    new Gyro\Bundle\MVCBundle\GyroMVCBundle(),
];


# src/App/Controller/DefaultController.php
namespace App\Controller;

class DefaultController
{
    public function helloAction($name = 'Fabien')
    {
        return ['name' => $name]; // :Default:hello.html.twig
    }
}


# src/App/Controller/DefaultController.php
namespace App\Controller;

use Gyro\MVC\TemplateView;

class DefaultController
{
    public function helloAction($name = 'Fabien')
    {
        return new TemplateView(
            ['name' => $name],
            'hallo', // :Default:hallo.html.twig instead of hello.html.twig
            201,
            ['X-Foo' => 'Bar']
        );
    }
}


# src/App/View/Default/HelloView.php
namespace App\View\Default;

class HelloView
{
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function getReversedName()
    {
        return strrev($this->name);
    }
}


# src/App/Controller/HelloController.php

namespace App\Controller;

class HelloController
{
    public function helloAction($name)
    {
        return new HelloView($name);
    }
}


# src/App/Controller/DefaultController.php
namespace App\Controller;

use Gyro\MVC\RedirectRoute;

class DefaultController
{
    public function redirectAction()
    {
        return new RedirectRoute('hello', ['name' => 'Fabien']);
    }
}


# src/App/Controller/DefaultController.php
namespace App\Controller;

use Gyro\MVC\Headers;
use Gyro\MVC\Flash;
use Symfony\Component\HttpFoundation\Cookie;

class DefaultController
{
    public function helloAction($name)
    {
        yield new Cookie('name', $name);
        yield new Headers(['X-Hello' => $name]);
        yield new Flash('warning', 'Hello ' . $name);

        return ['name' => $name];
    }
}

public function registerAction($request): RedirectRoute
{
    $user = $this->createUser($request);
    $this->entityManager->persist($user);

    yield new AfterResponseTask(fn () => $this->sendEmail($user));

    return new RedirectRoute('home');
}


# src/App/Controller/DefaultController.php
namespace App\Controller;

use Gyro\MVC\TokenContext;

class DefaultController
{
    public function redirectAction(TokenContext $context)
    {
        if ($context->hasToken()) {
            $user = $context->getCurrentUser(MyUser::class);
        } else if ($context->hasAnonymousToken()) {
            // do anon stuff
        }

        if ($context->isGranted('ROLE_ADMIN')) {
            // do admin stuff
            echo $context->getCurrentUserId();
            echo $context->getCurrentUsername();
        }
    }
}


# src/App/Controller/ProductController.php

namespace App\Controller;

use Gyro\MVC\FormRequest;
use Gyro\MVC\RedirectRoute;

class ProductController
{
    private $repository;

    public function __construct(ProductRepository $repository)
    {
        $this->repository = $repository;
    }

    public function editAction(FormRequest $formRequest, $id)
    {
        $product = $this->repository->find($id);

        if (!$formRequest->handle(new ProductEditType(), $product)) {
            return ['form' => $formRequest->createFormView(), 'entity' => $product];
        }

        $product = $formRequest->getValidData();

        $this->repository->save($product);

        return new RedirectRoute('Product.show', ['id' => $id]);
    }
}

public function indexAction(Session $session)
{
}

use Gyro\MVC\EventDispatcher\EventDispatcher;

class MyEvent
{
}

class MyService
{
    private EventDispatcher $eventDispatcher;

    public function __construct(EventDispatcher $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }

    public function performMyOperation()
    {
        // ....
        $this->eventDispatcher->dispatch(new MyEvent());
    }
}