An open API service indexing awesome lists of open source software.

https://github.com/themarlboroman/refresh-symfony

A Symfony 2.8 refresher to remember how stuff worked in a very basic way.
https://github.com/themarlboroman/refresh-symfony

remember symfony2 tutorial

Last synced: 5 months ago
JSON representation

A Symfony 2.8 refresher to remember how stuff worked in a very basic way.

Awesome Lists containing this project

README

        

Refresh Symfony
===============

A small project created to remember how Symfony 2.8 works and also serve as a reference... This is not meant to be a symfony crash course, but rather a refresher (hence the name).

It will cover the next steps:

- Creating the project and setting it up.
- How to use.
- Setting it up on GIT.
- Creating a quick controller.
- Using twig.
- Databases and doctrine.
- Custom services.
- Forms.
- Security and users.
- Creating console commands.

Each step will have examples of code in the repository. All work will be done with new examples so the code in the repository is always "final", that is, new features are introduced in new modules of the final application. It is recommended that the steps are followed one by one since the work is incremental. The idea is that all sections can be read in order and their instructions carried out so in the end we have some basic Symfony knowledge.

One more thing, topics are not restricted to their chapter (for example, we see some more twig features in the database chapter). Knowledge is just added incrementally.

# My setup.

I am using a Lubuntu 14 machine, with Xampp 1.8.3-2. That's PHP 5.5.6, Mysql 5.6.14 and Apache 2.4.7. I don't use the symfony built in server, but create my applications in the htdocs directory provided by Xampp.

# How to use.

The idea here is to follow the steps listed here and IGNORE the code provided by the repository... However, if you feel like you need to use the repository itself, then, by all means:

- cd into the repository.
- run composer update (usually like php -d memory_limit=-1 /path/to/composer update, without memory limit).
- feed composer the data it needs (like database user and such).
- fix the cache permissions (read on).

# Creating the project and setting it up.

First things first, this is how you create the project.

symfony new project_name version

For my version of PHP I need to go symfony 2.8 so...

symfony new refresh_symfony 2.8

Next it would be a good idea to check the configuration by navigating to:

localhost/refresh_symfony/web/config.php

It is likely that it will complain about the cache and log directories not being readable. Fix that, either by 0777 them or, if you are a subtle person, chowning them to the daemon user in the daemon group (mind you that this may give you problems down the line when you run console commands).

sudo chown daemon:daemon app/cache
sudo chown daemon:daemon app/logs

In any case, fix whatever you need from "config".

# Erasing unused files and entries.

There are a few files we are not going to use, so get rid of them:

- rm src/AppBundle/Controller/TestController.php
- rm -rf app/Resources/views/default/
- rm app/Resources/views/base.html.twig
- rm -rf src/AppBundle/Tests

Also, clean up the contents of - but don't delete - this file:

- app/config/routing.ytml

That's enough for now. There are a few full bundles and configuration entries we are not going to use, but let's not worry about that now. Later, if you feel the need, check out the doctrineless-symfony repository to learn about bundle removal.

# Setting it up on GIT...

This one is easy:

git init
git add whatever files you need
git commit -m 'Whatever commit message you want'
git remote add origin url_here
git push -u origin master

The -u is to set the tracking of your branches.

# Creating a quick controller.

This should be done in two parts: the route and the controller. Let us start with the route. Open the file at:

app/config/routing.ytml

You can add your route to it like this:

routename:
path: path-to-your-route
defaults: {_controller: BundleName:ControllerClassName:actionmethodname}

For example:

my-first-route:
path: this-is-my-first-route
defaults: {_controller:AppBundle:FirstRoute:showFirstRoute}

Now, remember that YAML doesn't want tabs there. Anyway, you can try and navigate to

http://localhost/refresh_symfony/web/app_dev.php/this-is-my-first-routename

And will be able to watch symfony complain of the non existing controllers... In particular it will say that there is no AppBundle\Controller\FirstRouteController so...

touch src/AppBundle/Controller/FirstRouteController.php

Notice how the filename matches both the class name we will use (FirstRoute) and the Controller word. Try and load again. This time it will complain that the controller class is not inside the file. Create it, you can use something like this:

Hello!