简体   繁体   中英

Symfony 2 Routing

I've got a problem with routing in my project.

I have 2 controllers, one is

Domestos\\TranslatingBundle\\ProjectController.php

p

and second is

Domestos\\TranslatingBundle\\LanguageController.php

Also i have routing set up with annotations.

routing.yml:

# import routes from a controller directory
translation:
    resource: "@DomestosTranslatingBundle/Controller/"
    type:     annotation

ProjectController works great, router is working, great.

Problem is, that my LanguageController is not working. Controller should be done ok, but always when i call URL with route predefined in annotation, it throw error

No route found for "GET /language/" 404 Not Found - NotFoundHttpException 1 linked Exception:

 ResourceNotFoundException » 

LanguageController.php looks like this:

<?php

namespace Domestos\TranslatingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Domestos\TranslatingBundle\Entity\Language;

class LanguageController extends Controller
{
    /**
     * @Route("/language")
     * @Template()
     */
    public function indexAction()
    {
        $languages = $this->getDoctrine()->getRepository('DomestosTranslatingBundle:Language')->findAll();

        return $this->render('DomestosTranslatingBundle:Language:index.html.twig', array(
            'languages' => $languages,
            ));
    }

    /**
     * @Route("/language//add")
     * @Template()
     */
    public function addAction()
    {
        $language = new Language();

        $language->setTitle('Jazyk')
                 ->setCode('Skratka');
        $form = $this->createFormBuilder($language)
                ->add('title', 'text')
                ->add('code', 'text')
                ->add('save', 'submit')
                ->getForm();

        $form->handleRequest($request);

        if($form->isSubmitted())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($language);
            $em->flush();
        }

        return $this->render('DomestosTranslatingBundle:Language:add.html.twig', array(
            'form' => $form->createView(),
            )); 
    }

    /**
     * @Route("/language/edit/{id}")
     * @Template()
     */
    public function editAction($id)
    {
        $language = $this->getDoctrine->getRepository('DomestosTranslatingBundle:Language')->find($id);

        $form = $this->createFormBuilder($language)
                ->add('title', 'text')
                ->add('code', 'text')
                ->add('save', 'submit')
                ->getForm();

        $form->handleRequest($request);

        if($form->isSubmitted())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($language);
            $em->flush();
        }

        return $this->render('DomestosTranslatingBundle:Language:edit.html.twig', array(
            'form' => $form->createView(),
            )); 
    }

    /**
     * @Route("/language/delete/{id}")
     * @Template()
     */
    public function deleteAction($id)
    {
        $language = $this->getDoctrine->getRepository('DomestosTranslatingBundle:Language')->find($id);
        $em = $this->getDoctrine()->getManager();
        $em->remove($language);
        $em->flush();

        return new Response('Language deleted: ' . $language->getCode());
    }

}

The question is, why is this controller not working? I need to finish project, and this is really annoying.

Ok, try with

/**
 * @Route("/language")
 */
class LanguageController extends Controller
{
    /**
     * @Route("/")
     * @Template()
     */
    public function indexAction()
    {
        // ...
    }

    /**
     * @Route("/add")
     * @Template()
     */
    public function addAction()
    {
        // ...
    }
    // And so on
}

Try stripping the tailing slash in:

resource: "@DomestosTranslatingBundle/Controller/"

to be:

resource: "@DomestosTranslatingBundle/Controller"

as seen as in: here

If that does not work, try running:

php app/console router:debug

to see all the routes...

Modify this

translation:
    resource: "@DomestosTranslatingBundle/Controller/"
    type:     annotation

in

translation:
    resource: "@DomestosTranslatingBundle/Controller/LanguageController.php"
    type:     annotation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM