doushi4956 2015-08-16 20:35
浏览 23
已采纳

需要协助Symfony 2.7创建第一页

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...

  • 写回答

3条回答 默认 最新

  • doujinai2183 2015-08-16 22:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。