I want to make a new page, so I created a new bundle to create a new route to another page but there is a issue with routes 'No route found for "GET/products" '
Here is my controller
<?php
namespace MyApp\ProductBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* #Route("/products")
*/
public function indexAction()
{
return $this->render('ProductBundle:Default:index.html.twig');
}
}
this is my route
product:
resource: '#ProductBundle/Controller/'
type: annotation
The route appear when I execute the command php bin/console debug:router like this :
product ANY ANY ANY /products
I execute the command server: start and then server:run that the probleme still not fixed
can any one help me please
Related
I'm really new with symfony, and I have strange problem.
I have a default controller, which looks like:
Both Controllers located:
src/AppBundle/Controller/
Names:
DefaultController.php
and
CmsController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* #Route("/")
*/
public function indexAction(Request $request)
{
die('Homepage');
}
}
And I'm trying to create new one:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class CmsController extends Controller
{
/**
* #Route("/cms")
*/
public function cmsAction(Request $request)
{
die('Cms Page');
}
}
Routing file looks like:
app:
resource: "#AppBundle/Controller/"
type: annotation
When I try to go for www.domainname.com - default controller shows "Homepage" - as it should.
When I try to go for www.domainname.com/cms - it gives error 404.
What can be the problem?
Probelm was simply with cache. Clearing it solved the problem.
I have added a second bundle and when I try to open some url from that bundle, I keep getting an error that the route was not found. Adding the same route to the main bundle works perfectly.
What's wrong?
This is my project structure. I also:
- added the UserBundle to AppKernel.php (IDE shows the class exists)
- use AppBundle\Controller namespace in AppBundle, and UserBundle\Controller namespace in UserBundle
The controller I try to access from the UserBundle looks like this:
namespace UserBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
/**
* #Route("/login", name="user_login")
*/
public function loginAction(Request $request)
{
return array();
}
}
I suspect you need to add the UserBundle to your routing.yml configuration. You should have something like this in the routing.yml:
user_bundle:
resource: "#UserBundle/Controller/"
type: annotation
// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function numberAction()
{
$number = rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
I'm new of Symfony framework. I tried this simple code without any result.
That's the server response:
No route found for "GET /lucky/number" 404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
I don't now why the Default controller use the annotation and I can see the homepage of my Symfony application.
I think you need to import your annotation routes. Symfony will scan locations you mention in app/config/routing.yml.
# app/config/routing.yml
lucky:
resource: "#AppBundle/Controller/LuckyController.php"
type: annotation
See Symfony docs for more details
Also try clearing the cache, just to be sure.
You're missing an important part of the route and its name:
/**
* #Route("/lucky/number", name="lucky_number")
*/
More information: http://symfony.com/doc/current/book/routing.html
I am new to symfony and following the AcmeDemo i created a new page for Contact Us. At the moment it is working for the url localhost:8000/contactus but following the same implementation of demo/secured/login it should be localhost:8000/demo/contactus which gives 404.
I am not sure what wrong i am doing.
routing.yml
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
ContactusController
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class ContactusController extends Controller
{
/**
* #Route("/contactus", name="_demo_contactus")
* #Template()
**/
public function indexAction()
{
return array();
}
}
?>
And then i have my view which is i am sure correct. Please provide the detailed answer so that it can help me in clearing my concepts as well. Thanks !!!
If you take a look at the SecuredController from the AcmeDemoBundle, you'll see that the class declaration has this annotation at the start:
/**
* #Route("/demo/secured")
*/
class SecuredController extends Controller
{
// ...
This annotation prefixes all subsequent routes in the Controller class with /demo/secured. Therefore, you should write your own Controller using the same fashion:
/**
* #Route("/demo")
*/
class ContactusController extends Controller
{
// ...
See the docs concerning Route Prefixing for more information
alternatively, you could also leave the ContactusController class alone and modify your route import instead:
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
prefix: /demo
You're also using the same route name twice, which means Symfony will reload routes and overwrite anything you had before for _demo_contactus. Try the following:
routing.yml
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
ContactusController
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* #Route("/demo/contactus")
*/
class ContactusController extends Controller
{
/**
* #Route("/", name="_demo_contactus_home")
* #Template()
**/
public function indexAction()
{
return array();
}
}
Make sure you try both /demo/contactus and /demo/contactus/.. it may be looking at that slash
I create a bundle and Entity inside the bundle. But when i try to get the entity or repository in controller using the entity manager it give an error that class not exist.
I try to debug using php app/console doctrine:mapping:info
it prompts every thing is correct. Output
Found 4 mapped entities:
[OK] Bitcoin\MyBundle\Entity\ProductCategory
[OK] Bitcoin\MyBundle\Entity\AdminUser
[OK] Bitcoin\MyBundle\Entity\Product
[OK] Bitcoin\MyBundle\Entity\User
My Controller code is as follows
<?php
namespace Bitcoin\MyBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Bitcoin\AdminBundle\Form\Login;
use Bitcoin\AdminBundle\Form\LoginValidate;
class LoginController extends Controller {
public function loginAction(Request $request) {
$user = $this->getDoctrine()->getRepository('User');
echo '<pre>';
prin_r(get_class_methods(get_class($user)));
die;
$pageData = array(
'name' => 'Login',
);
return $this->render('BitcoinAdminBundle:Login:login.html.twig', $pageData);
}
}
When i access this in browser the output is :-
i am previously worked with zend framework but new in symfony2. Any help will appreciate.
i find the issue and it is really a silly mistake.
For get an repository we should have to use the Bundle name with class name like this
$user = $this->getDoctrine()->getRepository('BitcoinMyBundle:User');