Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Add an artisan make:view command #241

Open
svenluijten opened this issue Oct 6, 2016 · 26 comments
Open

Add an artisan make:view command #241

svenluijten opened this issue Oct 6, 2016 · 26 comments

Comments

@svenluijten
Copy link

Adding a command like php artisan make:view would allow the user to generate view files via the command line instead of having to use their editor and/or filesystem.

Some options that may be interesting:

  • php artisan make:view products.index -- Generates products/index.blade.php.
  • php artisan make:view index --extends="_layouts.base" -- Extend an already existing view.
  • php artisan make:view products.index --section="content" -- Scaffold out a section.
  • php artisan make:view products --resource -- Generates an entire resource (products/index.blade.php, products/edit.blade.php, products/show.blade.php & products/create.blade.php).

These features would of course be limited to blade-only. If someone is using Twig (or another templating engine), they might appreciate an option like --extension to set the file extension:

  • php artisan make:view products --extension="html.twig"

I already have an implementation of this here which can be used as inspiration.

@Garbee
Copy link

Garbee commented Oct 6, 2016

This would be a fantastic addition. For alternate formats though, I think it would be best to have a --type option that would take a string for the type to be used (defaulting to blade). The available options would be registered in a class which would handle grabbing the template files and setting up the data appropriately for that syntax. That would allow --extends and --section to be programmable for other syntaxes as well.

@wotta
Copy link

wotta commented Oct 13, 2016

This is what I hoped to get implemented to.
But there should be at least some default code in.

Also it would be nice to automatically be able to pass data trough from the eloquent model.
Like so :
php artisan make:view website.content --extends="website._base" --section="content" --data="App\Users::all()" --appendData="content"

This would then scaffold out the following view :

@inject('data', 'App\User')
@extends("layouts.website.master")

@section("content")

    {{-- This name would be dynamic to the model name. --}}
    <h1>Users</h1>

    {{-- Here is the data we passed trough --}}
    <ul>

        {{-- Always keep the first letter from the variable. --}}
        @foreach ($data::all() as $d)

            {{-- Get the data you want here. --}}
            <li>{{ $d-> }}</li>
        @endforeach
    </ul>

@endsection

@svenluijten
Copy link
Author

I'm not a fan of that @concept-core. It should be kept simple and clean without too much overhead. Your idea could be neat for a package or own seperate command on a per-app basis, but it wouldn't really fit in the main Laravel codebase in my opinion.

@wotta
Copy link

wotta commented Oct 13, 2016

@svenluijten hmm I think your right in the way you say that.
But keep in mind they shot of this idea multiple times : laravel/framework#14517

@assaqqaf
Copy link

I like this one:

php artisan make:view products --resource

@kingRayhan
Copy link

kingRayhan commented Oct 30, 2017

What about this? I will write this command

php artisan make:view posts --resource

And generate a full set of necessary view files for a resource controller.
So it will generate these files

posts/index.blade.php
posts/create.blade.php
posts/store.blade.php
posts/show.blade.php
posts/edit.blade.php
posts/update.blade.php
posts/destroy.blade.php

@wotta
Copy link

wotta commented Oct 30, 2017

@kingRayhan that might be overkill, most of the times from what I have seen at least the only thing you need are the following views.
Index, create, edit and show.

Note that the create and edit could include a format view which hold the main form for both edit and create.

@kingRayhan
Copy link

@concept-core
ha ha,
Maybe I was over excited 😸

BTW: I found a third party artisan command package exactly what was I looking for.
You can check this.
https://github.com/svenluijten/artisan-view

@MadMikeyB
Copy link

+1 for baking this into core.

@svenluijten svenluijten changed the title Add an artisan make:view command [Proposal] Add an artisan make:view command Apr 4, 2018
@mikizdr
Copy link

mikizdr commented Jul 25, 2018

Already made solution:
https://github.com/svenluijten/artisan-view

@svenluijten
Copy link
Author

@mikizdr Yep! This was mentioned in my original issue (last sentence), and in @kingRayhan comment here. Thanks, though 🙂

@svenluijten svenluijten changed the title [Proposal] Add an artisan make:view command Add an artisan make:view command Dec 20, 2018
@taylorotwell
Copy link
Member

This command could balloon up with options pretty quick unless we intentionally limited the scope. I would personally limit it to a simple --extends switch... in a perfect world that would probably scan for @yields in that layout and automatically make sections for them in the newly generated view but that could be tricky? What if the yield is a variable... or some concatenated string, etc.

@wotta
Copy link

wotta commented Jan 28, 2019

@taylorotwell, limiting sounds a good way to go about this.
I wouldn't personally check if the yields exists to be honest.

Just a simple way to create view/resource views ( that where I would use it for ) would be sufficient I guess.

Thanks for replying and taking the time to share your opinion.

@taylorotwell
Copy link
Member

Yeah, sure, that's the most basic approach for sure.

@wotta
Copy link

wotta commented Jan 28, 2019

I feel like it doesn't need to be all that extensive. ( A year back I thought otherwise )

If I image that workflow along with the make:model -r command it would fit just how I would expect it.

@svenluijten
Copy link
Author

svenluijten commented Jan 28, 2019

Yep, I feel like the options in sven/artisan-view are pretty good as it is (though I might be biased 😉), minus maybe the --with-stacks and --with-yields options.

AFAIK no other make:* command currently "reads" other files to determine what should happen in the generated class/file, and I don't see a reason to introduce that overhead now. If added in an upcoming release, I propose it's kept simple and straight forward; we can always add options later on. Removing some if they get in the way, hovever, might prove more difficult.

@taylorotwell
Copy link
Member

This sorta raises the point... if sven/artisan-view is good, why do we want to duplicate the code in the core and grow the maintenance burden of the project? 😄

@svenluijten
Copy link
Author

Hah, good point! If not pulled into core, I'll still be maintaining Artisan View (some help from the community is always appreciated 😉), and at the end of the day it's your call @taylorotwell.

What does the rest here in the thread think?

@wotta
Copy link

wotta commented Jan 30, 2019

My 2 cents on this would be that I think a simple artisan make view command in the core would be nice. But nowadays it would not be something that I really need.

If I really need I can still pull your package in and use it.
But I still see some sort of workflow benefit if the framework had a simplistic view command in it.

I guess this is really a taste thing.
No real benefits to do implement it but also no user experience hits if it is in.

@Gigamick
Copy link

Gigamick commented Feb 1, 2019

My two cents:

artisan make:view contact --extends whatever

would be ideal. I think it could scope creep out of control so I'd be all for limiting to that basic functionality

@lechihuy
Copy link

lechihuy commented Apr 1, 2020

You can refer my tutorial: https://flipper.vn/tu-viet-lenh-make-view-trong-artisan-laravel

@elijahcruz12
Copy link

Is this still something that could be implemented? I love this idea so much! I tried to do this on my own using php artisan make:command CreateView but got very stuck.

@svenluijten
Copy link
Author

@elijahcruz12 Just use sven/artisan-view as mentioned several times above. It supports Laravel 7.x.

@akshaykadambatt
Copy link

It will be cool.

@starlabs-id
Copy link

Amazing, but if I create the same name blade, it would be replaced

@benjaminv
Copy link

what that too.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests