Coder Social home page Coder Social logo

laravel-simple-vm's Introduction

The Simple VM For Laravel

PHPStan Validate Composer Testing

This package is a wrap of Simple VM for Laravel.
You can also create a ViewModel class with a command.
Enjoy!

Installation

Execute the following composer command.

composer require takemo101/laravel-simple-vm

How to use

Please use as follows

Create a ViewModel class

Execute the artisan command as below.

# php artisan make:svm TestViewModel

Basic

1. Create a ViewModel class

First, create a ViewModel class to output the data to View.

<?php

namespace App\Http\ViewModel;

use Takemo101\LaravelSimpleVM\ViewModel;
use Takemo101\SimpleVM\Attribute\{
    Ignore,
    ChangeName,
};
use App\Models\User;
use Illuminate\Support\Collection;

/**
 * You need to extend and create the ViewModel class.
 * Only the public properties and methods defined in the ViewModel class will be the output target values.
 */
class TestViewModel extends ViewModel
{
    /**
     * ignores property or method names
     *
     * @var string[]
     */
    protected array $__ignores = [
        //
    ];

    /**
     * The data to be output to View is passed to the ViewModel class in the constructor.
     */
    public function __construct(
        public string $description,
        private User $user,
    ) {
        //
    }

    /**
     * Method injection is available for each published method
     */
    public function users(User $user): Collection
    {
        return $user->all();
    }

    /**
     * You can also process the model object and output it like the JsonResponse class.
     */
    public function user(): array
    {
        return [
            'name' => $this->user->name,
            'email' => $this->user->email,
        ];
    }

    /**
     * You can ignore the output to the View by setting the Ignore Attribute class
     */
    #[Ignore]
    public function other(): string
    {
        return 'other';
    }

    /**
     * You can change the output name to the View by setting the ChangeName Attribute class.
     */
    #[ChangeName('modification')]
    public function change(): string
    {
        return 'change';
    }
}

2. Use ViewModel class in Controller class

Next, use the created ViewModel class object as a response in the controller.

<?php

namespace App\Http\Controllers;

use App\Http\ViewModel\TestViewModel;
use App\Models\User;

class HomeController
{
    /**
     * Output Json by making the return value of the controller method an object of ViewModel
     */
    public function json()
    {
        $user = new User();
        $user->name = 'name';
        $user->email = '[email protected]';

        return new TestViewModel(
            'description',
            $user,
        );
    }

    /**
     * You can use the output data on the template by passing a ViewModel object as the template data
     */
    public function view()
    {
        $user = new User();
        $user->name = 'name';
        $user->email = '[email protected]';

        // By using the toAccessArray method, you can treat the output data like an object on the template.
        
        return view('home.view', (new TestViewModel(
            'description',
            $user,
        ))->toAccessArray());

        // You can create an object from the of method of the ViewModel class
    }
}

Below is the output result in Json.

{
	"description": "description",
	"users": [],
	"user": {
		"name": "name",
		"email": "[email protected]"
	},
	"modification": "change"
}

laravel-simple-vm's People

Contributors

hayama-kei avatar takemo101 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.