All Projects → tbreuss → yii2-inertia

tbreuss / yii2-inertia

Licence: MIT license
The Yii 2 server-side adapter for Inertia.js.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to yii2-inertia

pingcrm-yii2
Ping CRM on Yii 2 - A Yii 2 demo application to illustrate how Inertia.js works.
Stars: ✭ 39 (-25%)
Mutual labels:  yii2, inertiajs
yii2-s
🚀speed up yii2 tcp server、 restful by swoole
Stars: ✭ 34 (-34.62%)
Mutual labels:  yii2
inertiajs-tables-laravel-query-builder
Inertia.js Tables for Laravel Query Builder
Stars: ✭ 391 (+651.92%)
Mutual labels:  inertiajs
immer-adapter
🐹 Declarative state mutations
Stars: ✭ 47 (-9.62%)
Mutual labels:  adapter
yii2-stat
Yii2 Multi Web Statistic Module (yametrika, google-analytic, own db-counter)
Stars: ✭ 18 (-65.38%)
Mutual labels:  yii2
MultiTypeAdapter
RecyclerView通用多类型适配器MultiTypeAdapter,以布局文件为单位更细粒度的条目复用。
Stars: ✭ 18 (-65.38%)
Mutual labels:  adapter
yii2-translatable
Translatable behavior aggregates logic of linking translations to the primary model
Stars: ✭ 15 (-71.15%)
Mutual labels:  yii2
yii2-merchant
Payment merchants extension for Yii2
Stars: ✭ 17 (-67.31%)
Mutual labels:  yii2
poreplex
A versatile sequenced read processor for nanopore direct RNA sequencing
Stars: ✭ 74 (+42.31%)
Mutual labels:  adapter
atlas
Atlas: A React (Typescript), Laravel, Tailwind & Inertia starter kit. (Jetstream alternative)
Stars: ✭ 48 (-7.69%)
Mutual labels:  inertiajs
ember-airtable
Boilerplate for quickly prototyping apps with Airtable, Node & Ember
Stars: ✭ 21 (-59.62%)
Mutual labels:  adapter
yii2-basic-adminlte3
Yii 2 + AdminLTE 3 (Bootstrap 4) Yii2 Basic Project Template Build on AssetBundle (No CDN/Import CSS,JS Files)
Stars: ✭ 24 (-53.85%)
Mutual labels:  yii2
angular-yii2-model
AngularJS 1.x service to consume Yii2 RESTful API framework
Stars: ✭ 13 (-75%)
Mutual labels:  yii2
jetstream-inertia-generator
Laravel 8 Admin CRUD generator built with Jetstream, Inertia js, Vue 3 and Tailwindcss 2
Stars: ✭ 105 (+101.92%)
Mutual labels:  inertiajs
RecyclerAdapter
简单易懂的 RecyclerView adapter 封装
Stars: ✭ 27 (-48.08%)
Mutual labels:  adapter
phpstan-extensions
Extensions for PHPStan
Stars: ✭ 61 (+17.31%)
Mutual labels:  yii2
inertia
A preset for installing @inertiajs in a fresh Laravel project
Stars: ✭ 80 (+53.85%)
Mutual labels:  inertiajs
yii2-symfonymailer
Yii 2 Symfony mailer extension.
Stars: ✭ 29 (-44.23%)
Mutual labels:  yii2
ioBroker.zwave2
Z-Wave for ioBroker. Better. Faster. Stronger.
Stars: ✭ 22 (-57.69%)
Mutual labels:  adapter
Modular2Recycler
Modular²Recycler is a RecyclerView.Adapter that is modular squared.
Stars: ✭ 72 (+38.46%)
Mutual labels:  adapter

Inertia.js Yii 2 Adapter

This is the Yii 2 server-side adapter for Inertia.

With Inertia you are able to build single-page apps using classic server-side routing and controllers, without building an API.

To use Inertia you need both a server-side adapter as well as a client-side adapter.

Be sure to follow the installation instructions for the client-side framework you use.

Demo

https://pingcrm-yii2.tebe.ch

Installation

Composer require dependency:

composer require tebe/yii2-inertia

Edit config/web.php:

<?php

return [
    ...
    'bootstrap' => ['inertia']
    ...
    'components' => [
        'inertia' => [
            'class' => 'tebe\inertia\Inertia'
        ],
        'request' => [
            'cookieValidationKey' => '<cookie_validation_key>',
            'enableCsrfValidation' => false,
            'enableCsrfCookie' => false,
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ]            
        ]      
    ]
    ...
];   

Note that CSRF protection is disabled.

Controllers

Your backend controllers should extend from tebe\inertia\web\Controller. Instead of the render method within your actions you should use the inertia method.

<?php

namespace app\controllers;

use tebe\inertia\web\Controller;

class DemoController extends Controller
{
    public function actionIndex()
    {
        $params = [
            'data' => [],
            'links' => []
        ];
        return $this->inertia('demo/index', $params);
    }
}

Routing

Use your Yii server-side routes as usual. There is nothing special.

CSRF protection

Axios is the HTTP library that Inertia uses under the hood. Yii's CSRF protection is not optimized for Axios.

The easiest way to implement CSRF protection is using the customized tebe\inertia\web\Request component. Simply edit config/web.php file:

<?php

return [
    'components' => [
        'request' => [
            'class' => 'tebe\inertia\web\Request',             
            'cookieValidationKey' => '<cookie_validation_key>'
        ]      
    ]
];   

Please see the security page for more details.

Shared data

The Yii 2 adapter provides a way to preassign shared data for each request. This is typically done outside of your controllers. Shared data will be automatically merged with the page props provided in your controller.

Massive assignment of shared data:

<?php

$shared = [
    'user' => [
        'id' => $this->getUser()->id,
        'first_name' => $this->getUser()->firstName,
        'last_name' => $this->getUser()->lastName,
    ],
    'flash' => $this->getFlashMessages(),
    'errors' => $this->getFormErrors(),
    'filters' => $this->getGridFilters()
];
Yii::$app->get('inertia')->share($shared);

Shared data for one key:

<?php

$user = [
    'id' => $this->getUser()->id,
    'first_name' => $this->getUser()->firstName,
    'last_name' => $this->getUser()->lastName
];
Yii::$app->get('inertia')->share('user', $user);

A good strategy when using shared data outside of your controllers is to implement an action filter.

<?php

namespace app\components;

use yii\base\ActionFilter;

class SharedDataFilter extends ActionFilter
{
    public function beforeAction()
    {
        $shared = [
            'user' => $this->getUser(),
            'flash' => $this->getFlashMessages(),
            'errors' => $this->getFormErrors()
        ];
        Yii::$app->get('inertia')->share($shared);
        return parent::beforeAction($action);
    }
}    

And then use this action filter as a behaviour in your controller.

<?php

namespace app\controllers;

use app\components\SharedDataFilter;
use tebe\inertia\web\Controller;

class ContactController extends Controller
{
    public function behaviors()
    {
        return [
            [
                'class' => SharedDataFilter::class
            ]
        ];
    }
    
    public function actionIndex()
    {
        // your action code
    }
}

Please see the shared data page for more details.

Client-side setup

To use Inertia you need to setup your client-side framework. This primarily includes updating your main JavaScript file to boot the Inertia app. Please see the client-side setup page for more details.

More about Inertia

Visit inertiajs.com to learn more.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].