Application template

Initially when @hiqsol designed application template as multiple repositories:

// do not look the code, it’s outdated

yii-project-template serves the same purpose as Yii 2 basic or advanced template i.e. you start a project with it and once you did, code is yours.

The difference is other parts. These are dependencies of yii-project-template and aren’t owned by developer but by Yii framework. Thus:

  1. Could be updated.
  2. Could provide defaults.

While the idea is interesting, it seems to be very confusing:

So I am thinking about dropping this template dependencies and make it simpler like it is done in https://github.com/yiisoft/yii-demo.

What do you think?

Also, please choose one answer from the poll below:

    • Make it simpler
    • I like the idea
    • Not sure

0 voters

2 Likes

I am not sure about your question, but I have just been browsing the source for the yii-demo and I saw this in the SiteController:

public function index(): ResponseInterface
{
    $response = $this->responseFactory->createResponse();
    $output = $this->render('index');
    $response->getBody()->write($output);
    return $response;
}

I am a bit confused by this approach, as the current approach in Yii2 (and similarly in many other frameworks) is a lot more straightforward:

public function index()
{
    return $this->render('index');
}

Can you shed some light on this?

2 Likes

If my application have api, frontend and backend, how can I solve same code? In Yii2 advanced I can write in common, but how to do this in yii template mode?

1 Like

I think people can write a BaseController, and a render function to solve this, but this is not simpler

1 Like

That is request/response and middleware approach. The idea is documented.

In Yii 2 alike (but slightly limited) approach with request/response could be used.

The difference with simplified approach is that you’re getting request from Yii::$app->request, setting headers with Yii::$app->response and doing data/string returns (which, in fact are wrapped into response under the hood).

Similar way.

There are already dependencies to yii and its parts.
I am not sure what kind of dependencies you mean?

In any case, there should be examplery code for console, web and maybe rest api.
Maybe the web and restapi could be done with a Gii like generator. E.g. you want to add a blog, easy - goto app generator.

One issue you linked is about the user expecting a index.php, well no idea there. I think create-project should make something which usable from start, gives a better feeling specially for first time users. But could also be “yii init” script doing that.

The other issue is about libraries, using vue or vanilla js or angular - those differ drasticaly.
It would be nice if this is in a separate package, and not bundled.
If I make a console app, I dont need it, same for API. And if I want make web app I can use it then.
Vanilla JS should be default. And a external package which can enhance that.

Voting for make it simpler, multiple templates are tedious to mantain, and I dont see any advantage. I had say, make Gii generator for that.

1 Like

One key feature of (advanced at least) - deployability / maintainability. I added an API “section” (@api) to advanced in under an hour for example that pulls in all my core models in @common and just requires 1-2 controllers to build out a complete functional API. Could it be easier? Sure, but having a consistent interface for deployment and updates (checkout -> composer install -> ./yii migrate) should not be overlooked. The trouble is not starting a project - the trouble is not cratering it with mistakes after it is up and running.

The advantage to the current templates is that they have a consistent, maintained, explicit structure which is predictable enough that I can write Ansible scripts around it, for example. Just having a “demo” is not the same thing as having a deployable application I can hand to devops and not have to spend the next two weeks creating scaffolding from scratch…

The utility of the current templates should not be overlooked - they are actually fairly simple from that perspective vs. some other approaches I have seen, which actually makes the templates quite useful. If there is confusion due to the “complexity”, the answer is probably better docs or how-tos, not removing the feature.

Sure. Demo is just an example, it’s not a template that we’re going to ship with alpha version.

That’s all fine, but really I’m not sure if anyone would be happy doing it this way. Most people are used to doing it the “simplified” way, and I don’t just mean Yii users, I’m talking about Laravel, Symfony, Cake, etc.

Now when you create a new controller and want to render a view, you will probably not remember the render code and have to copy and paste it from another controller.

Yes, we can create out own render function that wraps all that logic up, but really we should not have to do this. I think many users will be put off by this approach.

This thing? I was going to move it to yii-web package.

The question is, what is simpler? Just to have a website by default?
The possibility to not have that was brought up in another design discussion.

Several users wanted to be able to start with no assumptions made in advance and ie. just create an API with a CLI.

Adding the web-part to the project template should just be one command, in theory :slight_smile:

It would also be nice, if yii-demo was created from yii-project-template which is AFAIK not the case.

It’s not the case because yii-project-template is currently outdated.

The question is whatever we prefer plain simple templates vs package inheritance. For example, yii-base-web will magically add some routes and their implementation to the application. yii-base-cli will add some console commands etc. In order to remove/adjust these you’ll have to override some configs.

I can see already questions, how can I enable cli in in my base-web, or how can I enable web in my cli?
If its possible to make it “combined” … would be nice.

1 Like

That’s why I dislike current template :frowning: base-web is not a template, it is used by template.

Yeah, base-web, base-api and other bases are merely entrypoints.

I’d vote for keeping the project-template without any code (like it is atm), but build examples like yii2-app-basic or yii2-app-advanced from it.

We could move on with generator approach. Then we could have one template, which is almost empty by default, but could easily generate endpoints using command line (provided by separate package). Like vendor/bin/yiiapp web frontend generates web endpoint under fronted directory and vendor/bin/yiiapp rest api will generate REST API endpoint under api directory. It could be even extended to use community templates (vendor/bin/yiiapp rob006/fancy-web-template fronted).

4 Likes

Or we can revert to plain template approach such as yii-app-basic. Without any base packages or composition or generation (except gii).