Olen  Predovic

Olen Predovic

1598839380

React Formik with Select Form Field # 20

Welcome to my channel Code Labs Javascript Learn Geek stuff [Angular, React, Redux, Nodejs, Web development, Docker, AWS, Vue JS, All about Javascript]. React Formik Course | forms in react js Full Playlist for React Forms Full Course

#react

What is GEEK

Buddha Community

React Formik with Select Form Field # 20
Autumn  Blick

Autumn Blick

1598839687

How native is React Native? | React Native vs Native App Development

If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?

In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.

A brief introduction to React Native

Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.

React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.

Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.

Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.

The popularity of React Native comes from its advantages. Some of its advantages are as follows:

  • Performance: It delivers optimal performance.
  • Cross-platform development: You can develop both Android and iOS apps with it. The reuse of code expedites development and reduces costs.
  • UI design: React Native enables you to design simple and responsive UI for your mobile app.
  • 3rd party plugins: This framework supports 3rd party plugins.
  • Developer community: A vibrant community of developers support React Native.

Why React Native is fundamentally different from earlier hybrid frameworks

Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.

React Native is very close to native. Consider the following aspects as described on the React Native website:

  • Access to many native platforms features: The primitives of React Native render to native platform UI. This means that your React Native app will use many native platform APIs as native apps would do.
  • Near-native user experience: React Native provides several native components, and these are platform agnostic.
  • The ease of accessing native APIs: React Native uses a declarative UI paradigm. This enables React Native to interact easily with native platform APIs since React Native wraps existing native code.

Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.

#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native

Mathew Rini

1615544450

How to Select and Hire the Best React JS and React Native Developers?

Since March 2020 reached 556 million monthly downloads have increased, It shows that React JS has been steadily growing. React.js also provides a desirable amount of pliancy and efficiency for developing innovative solutions with interactive user interfaces. It’s no surprise that an increasing number of businesses are adopting this technology. How do you select and recruit React.js developers who will propel your project forward? How much does a React developer make? We’ll bring you here all the details you need.

What is React.js?

Facebook built and maintains React.js, an open-source JavaScript library for designing development tools. React.js is used to create single-page applications (SPAs) that can be used in conjunction with React Native to develop native cross-platform apps.

React vs React Native

  • React Native is a platform that uses a collection of mobile-specific components provided by the React kit, while React.js is a JavaScript-based library.
  • React.js and React Native have similar syntax and workflows, but their implementation is quite different.
  • React Native is designed to create native mobile apps that are distinct from those created in Objective-C or Java. React, on the other hand, can be used to develop web apps, hybrid and mobile & desktop applications.
  • React Native, in essence, takes the same conceptual UI cornerstones as standard iOS and Android apps and assembles them using React.js syntax to create a rich mobile experience.

What is the Average React Developer Salary?

In the United States, the average React developer salary is $94,205 a year, or $30-$48 per hour, This is one of the highest among JavaScript developers. The starting salary for junior React.js developers is $60,510 per year, rising to $112,480 for senior roles.

* React.js Developer Salary by Country

  • United States- $120,000
  • Canada - $110,000
  • United Kingdom - $71,820
  • The Netherlands $49,095
  • Spain - $35,423.00
  • France - $44,284
  • Ukraine - $28,990
  • India - $9,843
  • Sweden - $55,173
  • Singapore - $43,801

In context of software developer wage rates, the United States continues to lead. In high-tech cities like San Francisco and New York, average React developer salaries will hit $98K and $114per year, overall.

However, the need for React.js and React Native developer is outpacing local labour markets. As a result, many businesses have difficulty locating and recruiting them locally.

It’s no surprise that for US and European companies looking for professional and budget engineers, offshore regions like India are becoming especially interesting. This area has a large number of app development companies, a good rate with quality, and a good pool of React.js front-end developers.

As per Linkedin, the country’s IT industry employs over a million React specialists. Furthermore, for the same or less money than hiring a React.js programmer locally, you may recruit someone with much expertise and a broader technical stack.

How to Hire React.js Developers?

  • Conduct thorough candidate research, including portfolios and areas of expertise.
  • Before you sit down with your interviewing panel, do some homework.
  • Examine the final outcome and hire the ideal candidate.

Why is React.js Popular?

React is a very strong framework. React.js makes use of a powerful synchronization method known as Virtual DOM, which compares the current page architecture to the expected page architecture and updates the appropriate components as long as the user input.

React is scalable. it utilises a single language, For server-client side, and mobile platform.

React is steady.React.js is completely adaptable, which means it seldom, if ever, updates the user interface. This enables legacy projects to be updated to the most new edition of React.js without having to change the codebase or make a few small changes.

React is adaptable. It can be conveniently paired with various state administrators (e.g., Redux, Flux, Alt or Reflux) and can be used to implement a number of architectural patterns.

Is there a market for React.js programmers?
The need for React.js developers is rising at an unparalleled rate. React.js is currently used by over one million websites around the world. React is used by Fortune 400+ businesses and popular companies such as Facebook, Twitter, Glassdoor and Cloudflare.

Final thoughts:

As you’ve seen, locating and Hire React js Developer and Hire React Native developer is a difficult challenge. You will have less challenges selecting the correct fit for your projects if you identify growing offshore locations (e.g. India) and take into consideration the details above.

If you want to make this process easier, You can visit our website for more, or else to write a email, we’ll help you to finding top rated React.js and React Native developers easier and with strives to create this operation

#hire-react-js-developer #hire-react-native-developer #react #react-native #react-js #hire-react-js-programmer

Hayden Slater

1599277908

Validating React Forms With React-Hook-Form

Validating inputs is very often required. For example, when you want to make sure two passwords inputs are the same, an email input should in fact be an email or that the input is not too long. This is can be easily done using React Hook From. In this article, I will show you how.

Required Fields

The most simple, yet very common, validation is to make sure that an input component contains input from the user. React Hook Form basic concept is to register input tags to the form by passing register() to the tag’s ref attribute. As we can see here:

#react-native #react #react-hook-form #react-hook

Controller Extra Bundle for Symfony2

ControllerExtra for Symfony2

This bundle provides a collection of annotations for Symfony2 Controllers, designed to streamline the creation of certain objects and enable smaller and more concise actions.

Reference

By default, all annotations are loaded, but any individual annotation can be completely disabled by setting to false active parameter.

Default values are:

controller_extra:
    resolver_priority: -8
    request: current
    paginator:
        active: true
        default_name: paginator
        default_page: 1
        default_limit_per_page: 10
    entity:
        active: true
        default_name: entity
        default_persist: true
        default_mapping_fallback: false
        default_factory_method: create
        default_factory_mapping: true
    form:
        active: true
        default_name: form
    object_manager:
        active: true
        default_name: form
    flush:
        active: true
        default_manager: default
    json_response:
        active: true
        default_status: 200
        default_headers: []
    log:
        active: true
        default_level: info
        default_execute: pre

ResolverEventListener is subscribed to kernel.controller event with priority -8. This element can be configured and customized with resolver_priority config value. If you need to get ParamConverter entities, make sure that this value is lower than 0. The reason is that this listener must be executed always after ParamConverter one.

Entity provider

In some annotations, you can define an entity by several ways. This chapter is about how you can define them.

By namespace

You can define an entity using its namespace. A simple new new() be performed.

/**
 * Simple controller method
 *
 * @SomeAnnotation(
 *      class = "Mmoreram\CustomBundle\Entity\MyEntity",
 * )
 */
public function indexAction()
{
}

By doctrine shortcut

You can define an entity using Doctrine shortcut notations. With this format you should ensure that your Entities follow Symfony Bundle standards and your entities are placed under Entity/ folder.

/**
 * Simple controller method
 *
 * @SomeAnnotation(
 *      class = "MmoreramCustomBundle:MyEntity",
 * )
 */
public function indexAction()
{
}

By parameter

You can define an entity using a simple config parameter. Some projects use parameters to define all entity namespaces (To allow overriding). If you define the entity with a parameter, this bundle will try to instance it with a simple new() accessing directly to the container ParametersBag.

parameters:

    #
    # Entities
    #
    my.bundle.entity.myentity: Mmoreram\CustomBundle\Entity\MyEntity
/**
 * Simple controller method
 *
 * @SomeAnnotation(
 *      class = "my.bundle.entity.myentity",
 * )
 */
public function indexAction()
{
}

Controller annotations

This bundle provide a reduced but useful set of annotations for your controller actions.

@CreatePaginator

Creates a Doctrine Paginator object, given a request and a configuration. This annotation just injects into de controller a new Doctrine\ORM\Tools\Pagination\Pagination instance ready to be iterated.

You can enable/disable this bundle by overriding active flag in configuration file config.yml

controller_extra:
    pagination:
        active: true

By default, if name option is not set, the generated object will be placed in a parameter named $paginator. This behaviour can be configured using default_name in configuration.

This annotation can be configured with these sections

Paginator Entity

To create a new Pagination object you need to refer to an existing Entity. You can check all available formats you can define it just reading the Entity Provider section.

<?php

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator page

You need to specify Paginator annotation the page to fetch. By default, if none is specified, this bundle will use the default one defined in configuration. You can override in config.yml

controller_extra:
    pagination:
        default_page: 1

You can refer to an existing Request attribute using ~value~ format, to any $_GET element by using format ?field? or to any $_POST by using format #field#

You can choose between Master Request or Current Request accessing to its attributes, by configuring the request value of the configuration.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/{foo}
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = "~foo~"
 * )
 */
public function indexAction(Paginator $paginator)
{
}

or you can hardcode the page to use.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator limit

You need to specify Paginator annotation the limit to fetch. By default, if none is specified, this bundle will use the default one defined in configuration. You can override in config.yml

controller_extra:
    pagination:
        default_limit_per_page: 10

You can refer to an existing Request attribute using ~value~ format, to any $_GET element by using format ?field? or to any $_POST by using format #field#

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/{foo}/{limit}
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = "~foo~",
 *      limit = "~limit~"
 * )
 */
public function indexAction(Paginator $paginator)
{
}

or you can hardcode the page to use.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1,
 *      limit = 10
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator OrderBy

You can order your Pagination just defining the fields you want to orderBy and the desired direction. The orderBy section must be defined as an array of arrays, and each array should contain these positions:

  • First position: Entity alias (Principal object is set as x)
  • Second position: Entity field
  • Third position: Direction
  • Fourth position: Custom direction map (optional)
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      orderBy = {
 *          {"x", "createdAt", "ASC"},
 *          {"x", "updatedAt", "DESC"},
 *          {"x", "id", 1, {
 *              0 => "ASC",
 *              1 => "DESC",
 *          }},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

With the third and fourth value you can define a map where to match your own direction nomenclature with DQL one. DQL nomenclature just accept ASC for Ascendant and DESC for Descendant.

This is very useful when you need to match a url format with the DQL one. You can refer to an existing Request attribute using ~value~ format, to any $_GET element by using format ?field? or to any $_POST by using format #field#

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/order/{field}/{direction}
 *
 * For example, some matchings...
 *
 * /myroute/paginate/order/id/1 -> ORDER BY id DESC
 * /myroute/paginate/order/enabled/0 - ORDER BY enabled ASC
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      orderBy = {
 *          {"x", "createdAt", "ASC"},
 *          {"x", "updatedAt", "DESC"},
 *          {"x", "~field~", ~direction~, {
 *              0 => "ASC",
 *              1 => "DESC",
 *          }},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

The order of the definitions will alter the order of the DQL query.

Paginator Wheres

You can define some where statements in your Paginator. The wheres section must be defined as an array of arrays, and each array should contain these positions:

  • First position: Entity alias (Principal object is set as x)
  • Second position: Entity field
  • Third position: Operator =, <=, >, LIKE...
  • Fourth position: Value to compare with
  • Fifth position: Is a filter. By default, false
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      wheres = {
 *          {"x", "enabled", "=", true},
 *          {"x", "age", ">", 18},
 *          {"x", "name", "LIKE", "Eferv%"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

You can refer to an existing Request attribute using ~value~ format, to any $_GET element by using format ?field? or to any $_POST by using format #field#

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/{field}
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      wheres = {
 *          {"x", "name", "LIKE", "~field~"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

You can use as well this feature for optional filtering by setting the last position to true. In that case, if the filter value is not found, such line will be ignored.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute?query=name%
 * This Controller matches pattern /myroute as well
 *
 * In both cases this will work. In the first case we will apply the where line
 * in the paginator. In the second case, we wont.
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      wheres = {
 *          {"x", "name", "LIKE", "?query?", true},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator Not Nulls

You can also define some fields to not null. Is same as wheres section, but specific for NULL assignments. The notNulls section must be defined as an array of arrays, and each array should contain these positions:

  • First position: Object (Principal object is set as x)
  • Second position: Field
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      notNulls = {
 *          {"x", "enabled"},
 *          {"x", "deleted"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator Left Join

You can do some left joins in this section. The leftJoins section must be defined as an array of array, where each array can have these fields:

  • First position: Entity alias (Principal object is set as x)
  • Second position: Entity relation (Address)
  • Third position: Relation identifier (a)
  • Fourth position: If true, this relation is added in select group. Otherwise, wont be loaded until its request (optional)
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      leftJoins = {
 *          {"x", "User", "u", true},
 *          {"x", "Address", "a", true},
 *          {"x", "Cart", "c"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator Inner Join

You can do some inner joins in this section. The innerJoins section must be defined as an array of array, where each array can have these fields:

  • First position: Entity alias (x)
  • Second position: Entity relation (Address)
  • Third position: Relation identifier (a)
  • Fourth position: If true, this relation is added in select group. Otherwise, wont be loaded until its request (optional)
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      innerJoins = {
 *          {"x", "User", "u", true},
 *          {"x", "Address", "a", true},
 *          {"x", "Cart", "c"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator Attributes

A nice feature of this annotation is that you can also inject into your controller a Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes instance with some interesting information about your pagination.

  • currentPage : Current page fetched
  • totalElements : Total elements given your criteria. If none criteria is defined in your configuration, this value will show all elements of a certain entity.
  • totalPages : Total pages you can fetch given a criteria.
  • limitPerPage: Maximum number of elements in each page.

To inject this object you need to define the "attributes" annotation field with the method parameter name.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      attributes = "paginatorAttributes",
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1,
 *      limit = 10
 * )
 */
public function indexAction(
    Paginator $paginator,
    PaginatorAttributes $paginatorAttributes
)
{
    $currentPage = $paginatorAttributes->getCurrentPage();
    $totalElements = $paginatorAttributes->getTotalElements();
    $totalPages = $paginatorAttributes->getTotalPages();
    $limitPerPage = $paginatorAttributes->getLimitPerPage();

}

Paginator Example

This is a completed example and its DQL resolution

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /paginate/nb/{limit}/{page}
 *
 * Where:
 *
 * * limit = 10
 * * page = 1
 *
 * @CreatePaginator(
 *      entityNamespace = "ControllerExtraBundle:Fake",
 *      page = "~page~",
 *      limit = "~limit~",
 *      orderBy = {
 *          { "x", "createdAt", "ASC" },
 *          { "x", "updatedAt", "DESC" },
 *          { "x", "id", "0", {
 *              "1" = "ASC",
 *              "2" = "DESC",
 *          }}
 *      },
 *      wheres = {
 *          { "x", "enabled" , "=", true }
 *      },
 *      leftJoins = {
 *          { "x", "relation", "r" },
 *          { "x", "relation2", "r2" },
 *          { "x", "relation5", "r5", true },
 *      },
 *      innerJoins = {
 *          { "x", "relation3", "r3" },
 *          { "x", "relation4", "r4", true },
 *      },
 *      notNulls = {
 *          {"x", "address1"},
 *          {"x", "address2"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

The DQL generated by this annotation is

    SELECT x, r4, r5
    FROM Mmoreram\\ControllerExtraBundle\\Tests\\FakeBundle\\Entity\\Fake x

    INNER JOIN x.relation3 r3
    INNER JOIN x.relation4 r4

    LEFT JOIN x.relation r
    LEFT JOIN x.relation2 r2
    LEFT JOIN x.relation5 r5

    WHERE enabled = ?where0
    AND x.address1 IS NOT NULL
    AND x.address2 IS NOT NULL

    ORDER BY createdAt ASC, id ASC

PagerFanta Add-on

This annotation can create a PagerFanta instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a Pagerfanta object instance.

use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Pagerfanta\Pagerfanta;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1,
 *      limit = 10
 * )
 */
public function indexAction(Pagerfanta $paginator)
{
}

KNPPaginator Add-on

This annotation can create a KNPPaginator instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a KNPPaginator object instance.

use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Knp\Component\Pager\Pagination\PaginationInterface;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1,
 *      limit = 10
 * )
 */
public function indexAction(PaginationInterface $paginator)
{
}

@LoadEntity

Loads an entity from your database, or creates a new one.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Entity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user"
 * )
 */
public function indexAction(User $user)
{
}

By default, if name option is not set, the generated object will be placed in a parameter named $entity. This behaviour can be configured using default_name in configuration.

You can also use setters in Entity annotation. It means that you can simply call entity setters using Request attributes.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\Address;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Entity(
 *      namespace = "MmoreramCustomBundle:Address",
 *      name  = "address"
 * )
 * @Entity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user",
 *      setters = {
 *          "setAddress": "address"
 *      }
 * )
 */
public function indexAction(Address $address, User $user)
{
}

When User instance is built, method setAddress is called using as parameter the new Address instance.

New entities are just created with a simple new(), so they are not persisted. By default, they will be persisted using configured manager, but you can disable this feature using persist option.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Entity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user",
 *      persist = false
 * )
 */
public function indexAction(User $user)
{
}

Entity Mapping

When you define a new Entity annotation, you can also request the mapped entity given a map. It means that if a map is defined, this bundle will try to request the mapped instance satisfying it.

The keys of the map represent the names of the mapped fields and the values represent their desired values. Remember than you can refer to any Request attribute by using format ~field~, to any $_GET element by using format ?field? or to any $_POST by using format #field#

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * This Controller matches pattern /user/edit/{id}/{username}
 *
 * @Entity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user",
 *      mapping = {
 *          "id": "~id~",
 *          "username": "~username~"
 *      }
 * )
 */
public function indexAction(User $user)
{
}

In this case, you will try to get the mapped instance of User with passed id. If some mapping is defined and any entity is found, a new EntityNotFoundException` is thrown.

Entity Mapping Fallback

So what if one ore more than one mapping references are not found? For example, you're trying to map the {id} parameter from your route, but this parameter is not even defined. Whan happens here? Well, you can assume then that you want to pass a new entity instance by using the mappingFallback.

By default, if mapping_fallback option is not set, the used value will be the parameter default_mapping_fallback defined in configuration. By default this value is false

Don't confuse with the scenario where you're looking for an entity in your database, all mapping references have been resolved, and the entity is not found. In that case, a common "EntityNotFound" exception will be thrown by Doctrine.

Lets see an example. Because we have enabled the mappingFallback, and because the mapping definition does not match the assigned route, we will return a new empty User entity.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * This Controller matches pattern /user/edit/{id}
 *
 * @LoadEntity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user",
 *      mapping = {
 *          "id": "~id~",
 *          "username": "~nonexisting~"
 *      },
 *      mappingFallback = true
 * )
 */
public function indexAction(User $user)
{
    // $user->getId() === null
}

Entity Repository

By default, the Doctrine entity manager provides the right repository per each entity (not the default one, but the right specific one). Although, you can define a custom repository to be used in your annotation by using the repository configuration.

/**
 * Simple controller method
 *
 * @CreateEntity(
 *      namespace = "MmoreramCustomBundle:User",
 *      mapping = {
 *          "id": "~id~",
 *          "username": "~username~"
 *      }
 *      repository = {
 *          "class" = "Mmoreram\CustomBundle\Repository\AnotherRepository",
 *      },
 * )
 */
public function indexAction(User $user)
{
}

By default, the method findOneBy will always be used, unless you define another one.

/**
 * Simple controller method
 *
 * @CreateEntity(
 *      namespace = "MmoreramCustomBundle:User",
 *      mapping = {
 *          "id": "~id~",
 *          "username": "~username~"
 *      }
 *      repository = {
 *          "class" = "Mmoreram\CustomBundle\Repository\AnotherRepository",
 *          "method" = "find",
 *      },
 * )
 */
public function indexAction(User $user)
{
}

Entity Factory

When the annotation considers that a new entity must be created, because no mapping information has been provided, or because the mapping fallback has been activated, by default a new instance will be created by using the namespace value.

This configuration block has three positions

  • class - factory class
  • method - Method to use when retrieving the object
  • static - Method is static

You can define the factory with a simple namespace

/**
 * Simple controller method
 *
 * @CreateEntity(
 *      namespace = "MmoreramCustomBundle:User",
 *      factory = {
 *          "class" = "Mmoreram\CustomBundle\Factory\UserFactory",
 *          "method" = "create",
 *          "static" = true,
 *      },
 * )
 */
public function indexAction(User $user)
{
}

If you want to define your Factory as a service, with the possibility of overriding namespace, you can simply define service name. All other options have the same behaviour.

parameters:

    #
    # Factories
    #
    my.bundle.factory.user_factory: Mmoreram\CustomBundle\Factory\UserFactory
/**
 * Simple controller method
 *
 * @CreateEntity(
 *      class = {
 *          "factory" = my.bundle.factory.user_factory,
 *          "method" = "create",
 *          "static" = true,
 *      },
 * )
 */
public function indexAction(User $user)
{
}

If you do not define the method, default one will be used. You can override this default value by defining new one in your config.yml. Same with static value

controller_extra:
    entity:
        default_factory_method: create
        default_factory_static: true

@CreateForm

Provides form injection in your controller actions. This annotation only needs a name to be defined in, where you must define namespace where your form is placed.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\AbstractType;

/**
 * Simple controller method
 *
 * @CreateForm(
 *      class = "\Mmoreram\CustomBundle\Form\Type\UserType",
 *      name  = "userType"
 * )
 */
public function indexAction(AbstractType $userType)
{
}

By default, if name option is not set, the generated object will be placed in a parameter named $form. This behaviour can be configured using default_name in configuration.

You can not just define your Type location using the namespace, in which case a new AbstractType element will be created. but you can also define it using service alias, in which case this bundle will return an instance using Symfony DI.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\AbstractType;

/**
 * Simple controller method
 *
 * @CreateForm(
 *      class = "user_type",
 *      name  = "userType"
 * )
 */
public function indexAction(AbstractType $userType)
{
}

This annotation allows you to not only create an instance of FormType, but also allows you to inject a Form object or a FormView object

To inject a Form object you only need to cast method value as such.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\Form;

/**
 * Simple controller method
 *
 * @CreateForm(
 *      class = "user_type",
 *      name  = "userForm"
 * )
 */
public function indexAction(Form $userForm)
{
}

You can also, using [SensioFrameworkExtraBundle][1]'s [ParamConverter][2], create a Form object with an previously created entity. you can define this entity using entity parameter.

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Route(
 *      path = "/user/{id}",
 *      name = "view_user"
 * )
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @CreateForm(
 *      class  = "user_type",
 *      entity = "user"
 *      name   = "userForm",
 * )
 */
public function indexAction(User $user, Form $userForm)
{
}

To handle current request, you can set handleRequest to true. By default this value is set to false

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Route(
 *      path = "/user/{id}",
 *      name = "view_user"
 * )
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @CreateForm(
 *      class         = "user_type",
 *      entity        = "user"
 *      handleRequest = true,
 *      name          = "userForm",
 * )
 */
public function indexAction(User $user, Form $userForm)
{
}

You can also add as a method parameter if the form is valid, using validate setting. Annotation will place result of $form->isValid() in specified method argument.

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Route(
 *      path = "/user/{id}",
 *      name = "view_user"
 * )
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @CreateForm(
 *      class         = "user_type",
 *      entity        = "user"
 *      handleRequest = true,
 *      name          = "userForm",
 *      validate      = "isValid",
 * )
 */
public function indexAction(User $user, Form $userForm, $isValid)
{
}

To inject a FormView object you only need to cast method variable as such.

<?php

use Symfony\Component\Form\FormView;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;

/**
 * Simple controller method
 *
 * @CreateForm(
 *      class = "user_type",
 *      name  = "userFormView"
 * )
 */
public function indexAction(FormView $userFormView)
{
}

@Flush

Flush annotation allows you to flush entityManager at the end of request using kernel.response event

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Flush;

/**
 * Simple controller method
 *
 * @Flush
 */
public function indexAction()
{
}

If not otherwise specified, default Doctrine Manager will be flushed with this annotation. You can overwrite default Manager in your config.yml file.

controller_extra:
    flush:
        default_manager: my_custom_manager

You can also override this value in every single Flush Annotation instance defining manager value

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Flush;

/**
 * Simple controller method
 *
 * @Flush(
 *      manager = "my_own_manager"
 * )
 */
public function indexAction()
{
}

If you want to change default manager in all annotation instances, you should override bundle parameter in your config.yml file.

controller_extra:
    flush:
        default_manager: my_own_manager

If any parameter is set, annotation will flush all. If you only need to flush one or many entities, you can define explicitly which entity must be flushed.

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

use Mmoreram\ControllerExtraBundle\Annotation\Flush;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @Flush(
 *      entity = "user"
 * )
 */
public function indexAction(User $user)
{
}

You can also define a set of entities to flush

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

use Mmoreram\ControllerExtraBundle\Annotation\Flush;
use Mmoreram\ControllerExtraBundle\Entity\Address;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @ParamConverter("address", class="MmoreramCustomBundle:Address")
 * @Flush(
 *      entity = {
 *          "user", 
 *          "address"
 *      }
 * )
 */
public function indexAction(User $user, Address $address)
{
}

If multiple @Mmoreram\Flush are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.

@ToJsonResponse

JsonResponse annotation allows you to create a Symfony\Component\HttpFoundation\JsonResponse object, given a simple controller return value.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\ToJsonResponse;

/**
 * Simple controller method
 *
 * @ToJsonResponse
 */
public function indexAction(User $user, Address $address)
{
    return array(
        'This is my response'
    );
}

By default, JsonResponse is created using default status and headers defined in bundle parameters. You can overwrite them in your config.yml file.

controller_extra:
    json_response:
        default_status: 403
        default_headers:
            "User-Agent": "Googlebot/2.1"

You can also overwrite these values in each @JsonResponse annotation.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\ToJsonResponse;

/**
 * Simple controller method
 *
 * @ToJsonResponse(
 *      status = 403,
 *      headers = {
 *          "User-Agent": "Googlebot/2.1"
 *      }
 * )
 */
public function indexAction(User $user, Address $address)
{
    return array(
        'This is my response'
    );
}

If an Exception is returned the response status is set by default to 500 and the Exception message is returned as response.

STATUS 500 Internal server error

{
    message : 'Exception message'
}

In case we use a HttpExceptionInterface the use the exception status code as status code. In case we launch this exception

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

...

return new NotFoundHttpException('Resource not found');

We'll receive this response

STATUS 404 Not Found

{
    message : 'Resource not found'
}

If the exception is being launched on an annotation (e.g. Entity annotation) remember to add the JsonResponse annotation at the beginning or at least before any annotation that could cause an exception.

If multiple @Mmoreram\JsonResponse are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.

@Log

Log annotation allows you to log any plain message before or after controller action execution

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
 * Simple controller method
 *
 * @Log("Executing index Action")
 */
public function indexAction()
{
}

You can define the level of the message. You can define default one if none is specified overriding it in your config.yml file.

controller_extra:
    log:
        default_level: warning

Every Annotation instance can overwrite this value using level field.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
 * Simple controller method
 *
 * @Log(
 *      value   = "Executing index Action",
 *      level   = @Log::LVL_WARNING
 * )
 */
public function indexAction()
{
}

Several levels can be used, as defined in [Psr\Log\LoggerInterface][6] interface

  • @Mmoreram\Log::LVL_EMERG
  • @Mmoreram\Log::LVL_CRIT
  • @Mmoreram\Log::LVL_ERR
  • @Mmoreram\Log::LVL_WARN
  • @Mmoreram\Log::LVL_NOTICE
  • @Mmoreram\Log::LVL_INFO
  • @Mmoreram\Log::LVL_DEBUG
  • @Mmoreram\Log::LVL_LOG

You can also define the execution of the log. You can define default one if none is specified overriding it in your config.yml file.

controller_extra:
    log:
        default_execute: pre

Every Annotation instance can overwrite this value using level field.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
 * Simple controller method
 *
 * @Log(
 *      value   = "Executing index Action",
 *      execute = @Log::EXEC_POST
 * )
 */
public function indexAction()
{
}

Several executions can be used,

  • @Mmoreram\Log::EXEC_PRE - Logged before controller execution
  • @Mmoreram\Log::EXEC_POST - Logged after controller execution
  • @Mmoreram\Log::EXEC_BOTH - Logged both

@Get

The Get annotation allows you to get any parameter from the request query string.

For a GET request like:

GET /my-page?foo=bar HTTP/1.1

You can can simply get the foo var using the GET annotation

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Get;

/**
 * Simple controller method
 *
 * @Get(
 *     path = "foo"
 * )
 */
public function indexAction($foo)
{
    // Use the foo var
}

You can also customize the var name and the default value in case the var is not sent on the query string.

For a GET request like:

GET /my-page HTTP/1.1

And this annotation

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Get;

/**
 * Simple controller method
 *
 * @Get(
 *     path = "foo",
 *     name = "varName",
 *     default = 'bar',
 * )
 */
public function indexAction($varName)
{
    // This would print 'bar'
    echo $varName;
}

@Post

The Post annotation allows you to get any parameter from the post request body.

For a POST request like:

POST /my-page HTTP/1.1
foo=bar

You can can simply get the foo var using the POST annotation

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Post;

/**
 * Simple controller method
 *
 * @Post(
 *     path = "foo"
 * )
 */
public function indexAction($foo)
{
    // Use the foo var
}

You can also customize the var name and the default value in case the var is not sent on the query string.

For a POST request like:

POST /my-page HTTP/1.1

And this annotation

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Post;

/**
 * Simple controller method
 *
 * @Post(
 *     path = "foo",
 *     name = "varName",
 *     default = 'bar',
 * )
 */
public function indexAction($varName)
{
    // This would print 'bar'
    echo $varName;
}

Custom annotations

Using this bundle you can now create, in a very easy way, your own controller annotation.

Annotation

The annotation object. You need to define the fields your custom annotation will contain. Must extends Mmoreram\ControllerExtraBundle\Annotation\Annotation abstract class.

<?php

namespace My\Bundle\Annotation;

use Mmoreram\ControllerExtraBundle\Annotation\Annotation;

/**
 * Entity annotation driver
 *
 * @Annotation
 * @Target({"METHOD"})
 */
final class MyCustomAnnotation extends Annotation
{
    /**
     * @var string
     *
     * Dummy field
     */
    public $field;
    
    /**
     * Get Dummy field
     *
     * @return string Dummy field
     */
    public function getField()
    {
        return $this->field;
    }
}

Resolver

Once you have defined your own annotation, you have to resolve how this annotation works in a controller. You can manage this using a Resolver. Must extend Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver; abstract class.

<?php

namespace My\Bundle\Resolver;

use Symfony\Component\HttpFoundation\Request;

use Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver;
use Mmoreram\ControllerExtraBundle\Annotation\Annotation;

/**
 * MyCustomAnnotation Resolver
 */
class MyCustomAnnotationResolver extends AnnotationResolver
{
    /**
     * Specific annotation evaluation.
     *
     * This method must be implemented in every single EventListener
     * with specific logic
     *
     * All method code will executed only if specific active flag is true
     *
     * @param Request          $request
     * @param Annotation       $annotation
     * @param ReflectionMethod $method
     */
    public function evaluateAnnotation(
        Request $request,
        Annotation $annotation,
        ReflectionMethod $method
    )
    {
        /**
         * You can now manage your annotation.
         * You can access to its fields using public methods.
         * 
         * Annotation fields can be public and can be acceded directly,
         * but is better for testing to use getters; they can be mocked.
         */
        $field = $annotation->getField();
        
        /**
         * You can also access to existing method parameters.
         * 
         * Available parameters are:
         * 
         * # ParamConverter parameters ( See `resolver_priority` config value )
         * # All method defined parameters, included Request object if is set.
         */
        $entity = $request->attributes->get('entity');
        
        /**
         * And you can now place new elements in the controller action.
         * In this example we are creating new method parameter
         * called $myNewField with some value
         */
        $request->attributes->set(
            'myNewField',
            new $field()
        );
        
        return $this;
    }

}

This class will be defined as a service, so this method is computed just before executing current controller. You can also subscribe to some kernel events and do whatever you need to do ( You can check Mmoreram\ControllerExtraBundle\Resolver\LogAnnotationResolver for some examples.

Definition

Once Resolver is done, we need to define our service as an Annotation Resolver. We will use a custom tag.

parameters:
    #
    # Resolvers
    #
    my.bundle.resolver.my_custom_annotation_resolver.class: My\Bundle\Resolver\MyCustomAnnotationResolver

services:
    #
    # Resolvers
    #
    my.bundle.resolver.my_custom_annotation_resolver:
        class: %my.bundle.resolver.my_custom_annotation_resolver.class%
        tags:
            - { name: controller_extra.annotation }

Registration

We need to register our annotation inside our application. We can just do it in the boot() method of bundle.php file.

<?php

namespace My\Bundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\Common\Annotations\AnnotationRegistry;

/**
 * MyBundle
 */
class ControllerExtraBundle extends Bundle
{

    /**
     * Boots the Bundle.
     */
    public function boot()
    {
        $kernel = $this->container->get('kernel');

        AnnotationRegistry::registerFile($kernel
            ->locateResource("@MyBundle/Annotation/MyCustomAnnotation.php")
        );
    }
}

Et voilà! We can now use our custom Annotation in our project controllers.


Download Details:

Author: mmoreram
Source Code: https://github.com/mmoreram/ControllerExtraBundle

License: MIT license

#symfony #php 

Franz  Becker

Franz Becker

1651604400

React Starter Kit: Build Web Apps with React, Relay and GraphQL.

React Starter Kit — "isomorphic" web app boilerplate   

React Starter Kit is an opinionated boilerplate for web development built on top of Node.js, Express, GraphQL and React, containing modern web development tools such as Webpack, Babel and Browsersync. Helping you to stay productive following the best practices. A solid starting point for both professionals and newcomers to the industry.

See getting started guide, demo, docs, roadmap  |  Join #react-starter-kit chat room on Gitter  |  Visit our sponsors:

 

Hiring

Getting Started

Customization

The master branch of React Starter Kit doesn't include a Flux implementation or any other advanced integrations. Nevertheless, we have some integrations available to you in feature branches that you can use either as a reference or merge into your project:

You can see status of most reasonable merge combination as PRs labeled as TRACKING

If you think that any of these features should be on master, or vice versa, some features should removed from the master branch, please let us know. We love your feedback!

Comparison

 

React Starter Kit

React Static Boilerplate

ASP.NET Core Starter Kit

App typeIsomorphic (universal)Single-page applicationSingle-page application
Frontend
LanguageJavaScript (ES2015+, JSX)JavaScript (ES2015+, JSX)JavaScript (ES2015+, JSX)
LibrariesReact, History, Universal RouterReact, History, ReduxReact, History, Redux
RoutesImperative (functional)DeclarativeDeclarative, cross-stack
Backend
LanguageJavaScript (ES2015+, JSX)n/aC#, F#
LibrariesNode.js, Express, Sequelize,
GraphQL
n/aASP.NET Core, EF Core,
ASP.NET Identity
SSRYesn/an/a
Data APIGraphQLn/aWeb API

Backers

♥ React Starter Kit? Help us keep it alive by donating funds to cover project expenses via OpenCollective or Bountysource!

lehneres Tarkan Anlar Morten Olsen Adam David Ernst Zane Hitchcox  

How to Contribute

Anyone and everyone is welcome to contribute to this project. The best way to start is by checking our open issues, submit a new issue or feature request, participate in discussions, upvote or downvote the issues you like or dislike, send pull requests.

Learn More

Related Projects

  • GraphQL Starter Kit — Boilerplate for building data APIs with Node.js, JavaScript (via Babel) and GraphQL
  • Membership Database — SQL schema boilerplate for user accounts, profiles, roles, and auth claims
  • Babel Starter Kit — Boilerplate for authoring JavaScript/React.js libraries

Support

License

Copyright © 2014-present Kriasoft, LLC. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.


Author: kriasoft
Source Code: https://github.com/kriasoft/react-starter-kit
License: MIT License

#graphql #react