Giter Site home page Giter Site logo

2amigos / yii2-app-template Goto Github PK

View Code? Open in Web Editor NEW
54.0 14.0 20.0 82 KB

Yii 2 Basic Application Template with ConfigKit.

Home Page: https://2amigos.us

License: Other

PHP 96.22% JavaScript 0.02% CSS 2.42% Batchfile 1.35%
php project-template boilerplate 2amigos yii2 yii2-application-template

yii2-app-template's Introduction

Yii 2 Project Template With ConfigKit

Packagist Version Latest Stable Version Total Downloads

This project is an example of what can be done with https://github.com/2amigos/yii2-config-kit.

It's a proposed application project template for Yii2 projects. This new template is perfect for projects of any size. Even though the Yii community recommends the usage of the advanced project template, but if we look carefully at that template you'll soon realize that what it has is simply three applications in one: backend / frontend / console. So, what's the difference between that template and this one? A shared common folder and another app? What stops you from doing the same with this type of starting point architecture? Right, absolutely nothing.

Our knowledge and experience with Yii has taught us that the advanced template is not really necessary to build anything that this template cannot also handle. Do you wish to create a super-admin application? Then simply add another folder wherever you wish and inject the shared resources from the src folder, the business or domain logic.

If you are using our proposed template then please take into consideration some recommendations from us:

  • Make sure your application logic is not within your models. Yes, avoid that MVC pattern recommendation to have thick models. You will surely end up with many God objects (monolithic models with huge amounts of code that become so hard to maintain that you'll regret the day you started coding them; especially, when you have to port that business logic to a better/newer architecture).
  • Keep your controllers thin, your models thinner and your views as clean of PHP and JS code as you can.
  • If your application is going to be large, please, do not use the events of your models even though they allow you to do so, make sure they are not being part of a large process. You will end up with a nightmare hard to test, hard to maintain and/or scale. Try to implement the observer pattern differently, allowing your pool of observer objects to be notified about those events. That will make your code easier to test.
  • Do not mix different code in your views. Please, please, do not use inline scripts and/or css do not inject javascript code in your PHP files, do not use the dynamic javascript jquery onload capabilities of the framework. I believe this approach is so wrong. Use it for small projects, but do not use it for large ones. Stick with separation of concerns, and keep js where it should be: at the frontend, within JS files... and do us all a favor, do not be a fashionist. Don't end up having your HTML templates in your javascript components, even if the library allows you to do so. K.I.S.S., my friends, K.I.S.S. all the way.
  • Use task managers, or module bundlers for your javascript files. We have added a .bowerrc and gulpfile.js to keep it as simple as possible. Yii Asset Bundlers are good and serve the purpose for small projects, but be honest, is this where the Web design trends are going nowadays?

Yii is a great framework, because of all the tools that it encapsulates. It has great flexibility and even though a lot of people complain about its inheritance madness and internal design, it is still a good robust framework. We just need to be very cautious with that power, as it could bring you more problems than advantages.

Directory Structure

app                 [ Yii's application related code: commands, components, controllers, bundles, models, modules, 
|                     views and widgets ]
├── assets          [ contains asset's definitions ]
├── commands        [ contains Console commands (Yii names them controllers) ]
├── controllers     [ contains Web controller classes ]
├── migrations      [ database migrations ]
├── models          [ contains model classes ]
├── views           [ contains application views ]
└── widgets         [ contains widget components ]
bin                 [ contains command-line executable scripts ]
bootstrap           [ contains bootstrap process files ]
config              [ contains application configuration files ]
public              [ contains Web application entry script + static resources ]
runtime             [ contains files generated during application's runtime ]
src                 [ contains domain business logic files. Portable code, free of Yii's code. Build your library here. ]
tests               [ contains codeception tests for your application ]

Requirements

The minimum requirement by this project template that your Web server supports PHP 5.4.0. But we highly recommend you use the latest PHP 7+.

Installation

Install via Composer

If you do not have Composer, you may install it by following the instructions at getcomposer.org.

You can then install this project template using the following command:

php composer.phar create-project --prefer-dist --stability=dev 2amigos/yii2-app-template your-site-name

Once the commands finish simply do the following:

cd your-site-name 
composer start-server 

You will then can access your application on http://localhost:8080. To stop the server, simply type on your terminal
composer stop-server and it will be stopped. You could also simply Ctrl+C to stop the script execution.

Please note, that the composer's commands included on this template have been tested on Linux | Mac environments, no Windows sorry.

Testing

Tests are located in tests directory. They are developed with Codeception PHP Testing Framework. By default there are 3 test suites:

  • unit
  • functional
  • acceptance

First run the test server. This command will inject the index-test.php script on your web root so you will be able to run your functional and acceptance tests:

composer start-test-server 

Then tests can be execute by running:

composer exec codecept run

Or, if you have codeception installed globally simply:

codecept run 

Clean code

We have added some development tools for you to check your work for clean code:

  • PHP mess detector: Takes a given PHP source code base and look for several potential problems within that source.
  • PHP code sniffer: Tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
  • PHP code fixer: Analyzes some PHP source code and tries to fix coding standards issues. Please, modify .php_cs to suit your needs.

And you should use them in that order.

Using php mess detector

Sample with all options available:

 ./vendor/bin/phpmd ./src text codesize,unusedcode,naming,design,controversial,cleancode

Using code sniffer

 ./vendor/bin/phpcs -s --report=source --standard=PSR2 ./

Using code fixer

We have added a PHP code fixer to standardize our code. It includes Symfony, PSR2 and some contributors rules.

./vendor/bin/php-cs-fixer fix ./src --config .php_cs

2amigOS!

yii2-app-template's People

Contributors

amigo-tabin avatar bahirul avatar flackovic avatar pana1990 avatar schmunk42 avatar tonydspaniard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-app-template's Issues

Enviroment tests

When i run the following unit test:

<?php

use SideKit\Config\ConfigKit;

class SideKitTest extends \Codeception\Test\Unit
{
    /**
     * @var \UnitTester
     */
    protected $tester;

    // tests
    public function testEnviroment()
    {
        expect(ConfigKit::env()->get('DATABASE_DSN_DB'))->equals('yii2_tests');
    }
}

it fails:

screenshot_1

Composer scripts

Composer script have 300secs timeout by default, i should indicate it in this line :

Once the commands finish simply do the following:

cd your-site-name 
composer start-server // here 

if you executes composer run start-server --timeout=0 it ignore timeout, further details composer/composer#4918.

Other way we can config yii serve in controllerMap property to set docroot by default.

'controllerMap' => [
     'serve' => [
           'class' => 'yii\console\controllers\ServeController',
           'docroot' => '@web',
      ]
],

// then
yii serve // it wors!

[ENH] Add vars in env to mailer config

I think is useful add the following vars in .env :

MAIL_HOST                     = 
MAIL_USERNAME           =  
MAIL_PASSWORD           =  
MAIL_PORT                     = 587
MAIL_ENCRYPTION         = tls

And

'transport' => [
             'class' => 'Swift_SmtpTransport',
             'host' => ConfigKit::env()->get('MAIL_HOST'),
             'username' => ConfigKit::env()->get('MAIL_USERNAME'),
             'password' => ConfigKit::env()->get('MAIL_PASSWORD'),
              'port' => ConfigKit::env()->get('MAIL_PORT'),
              'encryption' => ConfigKit::env()->get('MAIL_ENCRYPTION'),
],

The file or directory to be published does not exist: ../vendor/bower/jquery/dist

On default template installation, alias for bower is missing in test environment.

This results in errors

The file or directory to be published does not exist: path/to/project/vendor/bower/jquery/dist.

Solution is to put same alias that can be found in web conf into test conf.


'aliases' => [
        '@bower' => '@vendor/bower-asset',
    ]

YII_DEBUG and YII_ENV settings not being honoured

It is assumed that YII_DEBUG and YII_ENV constants are defined in .env file as environment variables. However .env settings are not honoured by Yii. Here's why:

In public/index.php we have:

require __DIR__ . '/../bootstrap/autoload.php';
require __DIR__ . '/../bootstrap/sidekit.php';

In bootstrap/autoload.php:

require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

In vendor/yiisoft/yii2/BaseYii.php (which is an ancestor for vendor/yiisoft/yii2/Yii.php):

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');

and finally in bootstrap/sidekit.php:

defined('YII_DEBUG') or define('YII_DEBUG', ConfigKit::env()->get('YII_DEBUG'));
defined('YII_ENV') or define('YII_ENV', ConfigKit::env()->get('YII_ENV'));

Since YII_DEBUG and YII_ENV have been already defined in BaseYii, these settings have no effect.
It is necessary for bootstrap/sidekit.php to load prior to vendor/yiisoft/yii2/Yii.php.

Too many dependencies

i think to remove The following dependencies in composer.json :

"vlucas/phpdotenv": "^2.4",
"league/container": "^2.2",
"symfony/finder": "^3.1",

sidekit/config already has this dependencies in her composer.json file.

isn't it?

Sorry for my english in advanced if you want i can write in spanish in the future issues.

Regards ;)

Skinny models, controllers and views - but fat what?

In your guidelines you tell us to have skinny controllers, lean models and slender views.

Business logic should not be in those.

Understood.

What you haven't done, however, is tell us where to put it. 😄

In components?

Structure models

That's great template!
I've had an idea what if you add extra folders for search models, form models, queries?

Something that will look like that
image

Missing alias @tests

I'm having some drawbacks with fixtures, if i set alias @tests it fix.

How reproduce issue :

1º create migrate post and run
2º create model of post
3º create fixture of post
4º execute yii fixture Post // this raise exception unfound namespace

Installation Error

When I using a command:
php composer.phar create-project --prefer-dist --stability=dev sidekit/yii2-app-basic your-site-name
OR
php composer.phar create-project --prefer-dist --stability=dev sidekit/yii2-app-template your-site-name

I'm getting the error:
[InvalidArgumentException]
Could not find package sidekit/yii2-app-basic with stability dev.

OR
[InvalidArgumentException]
Could not find package sidekit/yii2-app-template with stability dev.

I've never had that problem before and I've been downloading multiple projects via composer before.
What is the problem?

add alias @root

is root alias is defined ?
for now i set manual root alias with this on bootstrap aliases:
Yii::setAlias('@root', ConfigKit::config()->getRootPath());

autoload src sugestion name

We load src folder with App, when app it self already defined in yii2.
I think it will be good if we change App to src. It will prevent typo or confusing namespace name.
Also change App/Configuration to app/configuration will be good, use lowercase instead ucfirst folder name for standardization.

manage events

in the documentation of the usuario module there is an example to use an external events.php file, how and where can it be imported into the entry point?
Thank you

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.