Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a new Symfony project with the new directory structure?

Tags:

symfony

Until some days ago, it was possible to create a new Symfony project with the new (Symfony 3) directory structure. When running composer create-project symfony/framework-standard-edition path/ "2.5.*", Composer would ask the following question:

Would you like to use Symfony 3 directory structure? [y/N]

The new structure offered some improvements, such as console being moved from the app directory to the bin directory, and phpunit.xml.dist being moved from the app directory to the root directory. The cache and logs directories were moved to a new var directory.

Take a look at this answer (written by me) for a full list of changes.

However, a fresh install of the standard distribution no longer offers this option. It seems like the question has been removed on July 16th, because the new directory structure created too much confusion, especially for new users. See this issue on GitHub as well.

Is it still possible to create a project using the new directory structure?

like image 685
Nic Wortel Avatar asked Jul 25 '14 13:07

Nic Wortel


People also ask

Is Symfony full stack?

It's common to call the Symfony framework a Full Stack Framework. This goes back to the days of it providing everything on the server side, from templating with Twig to data persistence by integrating the Doctrine ORM.

Is Symfony frontend or backend?

Symfony was the backend framework with the most contributors in 2019 (Symfony Blog)

What is MVC in Symfony?

The MVC Pattern. Symfony is based on the classic web design pattern known as the MVC architecture, which consists of three levels: The Model represents the information on which the application operates--its business logic. The View renders the model into a web page suitable for interaction with the user.


3 Answers

It is still possible to trigger the question, and convert a project to the new directory structure. (but only if you are creating a new project, i.e. running composer create-project)

To do so, you need to set the SENSIOLABS_ENABLE_NEW_DIRECTORY_STRUCTURE environment variable to true. This can be done by prepending SENSIOLABS_ENABLE_NEW_DIRECTORY_STRUCTURE=true to the Composer command.

So in order to create a new project, run the following command in your terminal:

SENSIOLABS_ENABLE_NEW_DIRECTORY_STRUCTURE=true composer create-project symfony/framework-standard-edition path/ "2.5.*"

and Composer will ask you if you want the new directory structure.

Update

As noted by barius in the comments, this feature has been removed from the Symfony Standard Edition per version 2.7.5. If you really want to use the Symfony 3 structure, you can get it by installing Symfony in 2 steps:

  1. Create a new Symfony project with a version constraint so you get the 2.6 version, which still asks you whether you want to use the new directory structure.
  2. Then change the version constraint for the symfony/symfony package so you'll still get the latest version.

So, execute the following commands from the command line:

SENSIOLABS_ENABLE_NEW_DIRECTORY_STRUCTURE=true composer create-project symfony/framework-standard-edition project-directory/ "2.6.*"
cd project-directory
composer require symfony/symfony ^2.7

Note: I don't actually recommend this, as this is not the official recommended way to create a new Symfony project. So unless you really know what you're doing, just use the Symfony Installer to create new projects.

like image 151
Nic Wortel Avatar answered Oct 07 '22 19:10

Nic Wortel


to create the new symfony3 directory structure in symfony2 (>=2.5) you have to do the following:

open your shell/command prompt and run the follwoing command to set a temporary environment variable:

linux:

export SENSIOLABS_ENABLE_NEW_DIRECTORY_STRUCTURE=true

windows:

set SENSIOLABS_ENABLE_NEW_DIRECTORY_STRUCTURE=true

version 2.5 and 2.6

the question, for creating the new symfony3 directory structure, is asked out of the box after setting the environment variable

version 2.7 and 2.8

to create the new directory structure from any 2.7 and 2.8 version, the following steps are necessary:

install the 2.7 and up version you want (tested with 2.7 and 2.8), which currently installs 2.7.7

composer create-project symfony/framework-standard-edition myproject "2.7.*"

then edit the composer.json and add a update-directory-structure:

composer.json:

--- snip ---
"scripts": {
    "post-install-cmd": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
    ],
    "post-update-cmd": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
    ],
    "update-directory-structure": [
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::defineDirectoryStructure"
    ]
},
--- snip ---

after that just run

composer run-script update-directory-structure

answer with 'y' and you are done:

> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::defineDirectoryStructure
Would you like to use Symfony 3 directory structure? [y/N]

the console file, which is now located in bin/console has to be adapted:

change

$loader = require __DIR__.'/autoload.php';

to

$loader = require __DIR__.'/../app/autoload.php';

then add the following methods to the AppKernel class, in the file app/appKernel.php:

public function getRootDir()
{
    return __DIR__;
}

public function getCacheDir()
{
    return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
}

public function getLogDir()
{
    return dirname(__DIR__).'/var/logs';
}

update: knp university article about migrating to the new directory stucture

credits: @davil

like image 31
c33s Avatar answered Oct 07 '22 19:10

c33s


Symfony3 BETA1 version is Released now,

So you can use composer directly to install new symfony3 version.

Follow Installing Composer first if you don't have it already installed.

Then open terminal and type following command:

composer create-project symfony/framework-standard-edition symfony3 "v3.0.0-BETA1"

Note: here symfony3 is name of project or directory which will be created, you can change that.

Update: Symfony 3.0 is stable and current version so one can directly install it as stated in Official guide. Follow it for latest version installation.

composer create-project symfony/framework-standard-edition my_project_name

You may like to check these links,

What is the new Symfony 3 directory structure?

Are your favorite bundle ready for #Symfony3? posted by @FecaBouffe

like image 30
Straw Hat Avatar answered Oct 07 '22 18:10

Straw Hat