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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么