Need Assistance with Symfony 2.7 creating first page

I am trying to learn the Symfony framework and struggling with it. The instructions are not very helpful or assume I know a lot more than I know. I am just trying to create a single web page with proper route and controller. I have been googling to find answers and made some progress but no luck yet. Right now I just have the standard install of Symfony with just default bundles etc. I created a project called "gtest3" and chose PHP for it...

I am not sure where I put the new route in (what file) or maybe it needs to be put in more than one file?

I found the "routing.yml" file which seems that is where I need to put it...

here is what is in there right now:

gtest3:
    resource: "@gtest3Bundle/Resources/config/routing.php"
    prefix:   /


app:
    resource: "@AppBundle/Controller/"
    type:     annotation

I am guessing I need to add something to this and put the location/filename of the controller? I have tried doing this a few ways and just get errors.

There is also the "routing.php" file that is referenced in the above code. I am not sure if this is the "controller" or if it is an additional piece of the "route". Here is the code from that file:

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$collection = new RouteCollection();

$collection->add('gtest3_homepage', new Route('/hello/{name}', array(
'_controller' => 'gtest3Bundle:Default:index',
)));

return $collection;

I am not sure what if anything I would add here. Finally - there is the "DefaultConroller.php" file I found as well which may also be the controller. I dont think I need to include the code of that file here.

So - all I am trying to do is create a route of maybe "/gman" and then have the controller just echo something on the page. Super basic stuff. And I cannot figure out how to get this going.

Can anyone help with this? Thanks so much...

Answers


You can define your routes in three ways, either by using yml files, xml files, or by using a php file. This is documented behaviour.

You are from the looks of your routing.yml trying to set up a php version. I would not recommend it, and instead use configuration over coding the routing.

The annotation example would look like:

Adding a controller:

namespace Gtest3Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class Gtest3Bundle extends Controller
{
/**
 * @Route("/hello/{name}")
 * @Template()
 */
public function indexAction($name)
{
    return array('name' => $name);
}
}

And add in your app/config/routing.yml:

gtest3:
    resource: "@Gtest3Rights/Controller/"
    type:     annotation
    prefix:   /what-ever-you-want

You can check what kind of routes you have defined by using:

./app/console router:debug

If it doesn't appear hear, you have something misconfigured.

Please be advised that your bundle names breaks the convention of how bundles should be named in the symfony2 context.

It is advised to use NamespaceProjectBundle. This also documented. If you are stuck, try generating a bundle via the ./app/console generate:bundle. This way you can create a whole symfony2 structure which should show the default page hello/foo page just fine.

If it doesn't seem to run at all, make sure you have registered your bundle at the in the app/AppKernel.php file in the registerBundles() method.


To configure routes you can use yml, php or xml file. You can specify it in app/config/config.yml

framework:
router:
    resource: "%kernel.root_dir%/config/routing.yml"

It's where you can check which file is used now.


There are some manners to store the routes.

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

This is how you use routes by writing annotations above your actions (eg indexAction) inside your controller class. Read more.

Another common approach is to build one or more .yml files inside each Bundle. in app/config/routing.yml you should write this:

app:
    resource: "@AppBundle/Resources/config/routing.yml"
    prefix:   /

Then you need to create the file (and directories if necessary) src/AppBundle/Resources/config/routing.yml and add the following content:

app_homepage:
    path:     /
    defaults: { _controller: AppBundle:Default:index }

It will then try to find the DefaultController and fires the indexAction in the AppBundle (src/AppBundle/Controller/DefaultController.php).

To debug your routes simply type in your console from your projects directory:

app/console router:debug

Need Your Help

Best way in Converting DataGrid View to DataSet/DataTable

c# winforms reporting-services rdlc localreport

DataSet, DataTable and linq expression are common DataSource values of a DataGridView.

Thinking Sphinx returns “bad multi-query count 0 (must be in 1..32 range)”

ruby-on-rails ruby-on-rails-3 sphinx thinking-sphinx

I'm trying to use Sphinx (2.0.1 beta) with Thinking Sphinx 2.0.4 (on Rails 3.0.3). I have one model so far that I'd like to add to my index and the indexing has worked. I can also start searchd thr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.