single: Page creation; Environments & Front Controllers
Every Symfony application runs within an environment
. An environment is a specific set of configuration and loaded bundles, represented by a string. The same application can be run with different configurations by running the application in different environments. Symfony comes with three environments defined — dev
, test
and prod
— but you can create your own as well.
Environments are useful by allowing a single application to have a dev environment built for debugging and a production environment optimized for speed. You might also load specific bundles based on the selected environment. For example, Symfony comes with the WebProfilerBundle (described below), enabled only in the dev
and test
environments.
Symfony comes with two web-accessible front controllers: app_dev.php
provides the dev
environment, and app.php
provides the prod
environment. All web accesses to Symfony normally go through one of these front controllers. (The test
environment is normally only used when running unit tests, and so doesn't have a dedicated front controller. The console tool also provides a front controller that can be used with any environment.)
When the front controller initializes the kernel, it provides two parameters: the environment, and also whether the kernel should run in debug mode. To make your application respond faster, Symfony maintains a cache under the app/cache/
directory. When debug mode is enabled (such as app_dev.php
does by default), this cache is flushed automatically whenever you make changes to any code or configuration. When running in debug mode, Symfony runs slower, but your changes are reflected without having to manually clear the cache.
Note
The tutorial assumes that you've already downloaded Symfony and configured your webserver. The above URL assumes that localhost
points to the web
directory of your new Symfony project. For detailed information on this process, see the documentation on the web server you are using. Here are some relevant documentation pages for the web server you might be using:
- For Apache HTTP Server, refer to Apache's DirectoryIndex documentation
- For Nginx, refer to Nginx HttpCoreModule location documentation
Before you begin, you'll need to create a bundle. In Symfony, a bundle
is like a plugin, except that all the code in your application will live inside a bundle.
A bundle is nothing more than a directory that houses everything related to a specific feature, including PHP classes, configuration, and even stylesheets and JavaScript files (see page-creation-bundles
).
Depending on the way you installed Symfony, you may already have a bundle called AcmeDemoBundle. Browse the src/
directory of your project and check if there is a DemoBundle/
directory inside an Acme/
directory. If those directories already exist, skip the rest of this section and go directly to create the route.
To create a bundle called AcmeDemoBundle (a play bundle that you'll build in this chapter), run the following command and follow the on-screen instructions (use all the default options):
$ php app/console generate:bundle --namespace=Acme/DemoBundle --format=yml
Behind the scenes, a directory is created for the bundle at src/Acme/DemoBundle
. A line is also automatically added to the app/AppKernel.php
file so that the bundle is registered with the kernel:
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Acme\DemoBundle\AcmeDemoBundle(),
);
// ...
return $bundles;
}
Now that you have a bundle setup, you can begin building your application inside the bundle.
Tip
You can also view your app in the "prod" environment <environments-summary>
by visiting:
http://localhost/app.php/random/10
If you get an error, it's likely because you need to clear your cache by running:
$ php app/console cache:clear --env=prod --no-debug
The controller renders the AcmeDemoBundle:Random:index.html.twig
template, which uses the following naming convention:
BundleName:ControllerName:TemplateName
This is the logical name of the template, which is mapped to a physical location using the following convention.
/path/to/BundleName/Resources/views/ControllerName/TemplateName
In this case, AcmeDemoBundle is the bundle name, Random
is the controller, and index.html.twig
the template:
The web root directory is the home of all public and static files including images, stylesheets, and JavaScript files. It is also where each front controller
lives:
// web/app.php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel->handle(Request::createFromGlobals())->send();
The front controller file (app.php
in this example) is the actual PHP file that's executed when using a Symfony application and its job is to use a Kernel class, AppKernel
, to bootstrap the application.
Tip
Having a front controller means different and more flexible URLs than are used in a typical flat PHP application. When using a front controller, URLs are formatted in the following way:
http://localhost/app.php/random/10
The front controller, app.php
, is executed and the "internal:" URL /random/10
is routed internally using the routing configuration. By using Apache mod_rewrite
rules, you can force the app.php
file to be executed without needing to specify it in the URL:
http://localhost/random/10
Though front controllers are essential in handling every request, you'll rarely need to modify or even think about them. They'll be mentioned again briefly in the Environments section.
As you saw in the front controller, the AppKernel
class is the main entry point of the application and is responsible for all configuration. As such, it is stored in the app/
directory.
This class must implement two methods that define everything that Symfony needs to know about your application. You don't even need to worry about these methods when starting - Symfony fills them in for you with sensible defaults.
registerBundles()
Returns an array of all bundles needed to run the application (see
page-creation-bundles
).registerContainerConfiguration()
Loads the main application configuration resource file (see the Application Configuration section).
In day-to-day development, you'll mostly use the app/
directory to modify configuration and routing files in the app/config/
directory (see Application Configuration). It also contains a directory for application-level resource files, such as templates (app/Resources
). You'll learn more about each of these directories in later chapters.
Congratulations! You've now seen every fundamental aspect of Symfony and have hopefully discovered how easy and flexible it can be. And while there are a lot of features still to come, be sure to keep the following basic points in mind:
- Creating a page is a three-step process involving a route, a controller and (optionally) a template;
- Each project contains just a few main directories:
web/
(web assets and the front controllers),app/
(configuration),src/
(your bundles), andvendor/
(third-party code) (there's also abin/
directory that's used to help updated vendor libraries); - Each feature in Symfony (including the Symfony Framework core) is organized into a bundle, which is a structured set of files for that feature;
- The configuration for each bundle lives in the
Resources/config
directory of the bundle and can be specified in YAML, XML or PHP; - The global application configuration lives in the
app/config
directory; - Each environment is accessible via a different front controller (e.g.
app.php
andapp_dev.php
) and loads a different configuration file.
From here, each chapter will introduce you to more and more powerful tools and advanced concepts. The more you know about Symfony, the more you'll appreciate the flexibility of its architecture and the power it gives you to rapidly develop applications.