Routing issue with Symfony 3.4 - symfony

I'm trying to access through localhost (Xampp) the index file from the folder in a Symfony project. Although i'm writing the following path it cannot find the page: http://localhost/google_analytics_app/consolytics-app/analytics/
tree-folder-structure
Following the instructions i use the base file in my index.html.twig file under the folder viwes/analytics/
{% extends 'base.html.twig' %}
and use my own index file under the folder/analytics/index.html.twig.
I also used annotations inside the src/AppBundle/Controller/IndexController.php :
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Library\GoogleAnalytics;
use Doctrine\Common\Util\Debug as dump;
class IndexController extends Controller
{
/**
* #Route("/analytics/index", name="homepage")
*/
public function indexAction(Request $request)
{
....
return $this->render('index.html.twig');
}
}
When type the path it shows 404 error message. Can someone help with this routing issue? In my composer.json file:
"require": {
"twig/twig": "^1.0||^2.0"
},

The problem is that You didn't register your routings in routing.yml file.
You have to understand that annotations are describe routes, but your router component don't now anything about your routes before You register that one in routing.yml file. From this file (v3.4) router gets your routes.
https://symfony.com/doc/3.4/best_practices/controllers.html#routing-configuration
# app/config/routing.yml
app:
resource: '#AppBundle/Controller/'
type: annotation
Pay attantion for type parameter. Annotation says to router that your routes are actually annotations :)

Your param for the render-method is wrong. It has to be return
$this->render('views/analytics/index.html.twig');

Related

No route found for "GET /lucky/number"

I've already looked at other posts here, none of their solutions worked.
Tried prefixing "<\?php" to the file,
Tried change clearing the cache.
None have worked.
Working on a fresh install of Ubuntu 16.04.3 LTS.
Running within the NetBeans IDE
LuckyNumberController.php is below:
<?php
// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number",name="lucky")
*/
public function numberAction()
{
$number = rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
Routing.yml below:
app:
resource: #AppBundle/Controller/
type: annotation
homepage:
path: /
defaults:
_controller: FrameworkBundle:Template:template
template: 'default/homepage.html.twig'
lucky:
path: /lucky/number
controller: App\Controller\LuckyController::numberAction
Actually both your declarations might be wrong.
Annotation
In your controller you're using
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
and according to documentation, it seems to be correct for Symfony <=3.3 but wrong for Symfony >=3.4 which should be
use Symfony\Component\Routing\Annotation\Route;.
Yaml
In your yaml file you're specifying
controller: App\Controller\LuckyController::numberAction
while actually your Controller's namespace is AppBundle\Controller; so your declaration should be
controller: AppBundle\Controller\LuckyController::numberAction
First of all, you don't need annotations routing and yml routing. You have to pick just one. I preferred yml version
Delete
...
/**
* #Route("/lucky/number",name="lucky")
*/
...
or you can delete this one
...
lucky:
path: /lucky/number
controller: App\Controller\LuckyController::numberAction
...
After that, it will work.
EDIT1:
If you are using Symfony 4 I recommended use yaml version (second one) because this one comes with (Symfony)
EDIT2:
I mean don't be confuse when you are creating new route.

symfony3 REST API issue with yml routing using FOSRestBundle

I've finally got my first API in Symfony3 which is actually working.
Very well so far but problems came when I tried using YML configs for routing instead of annotation. It is driving me crazy because it seems to work, in fact when I chance for eg the controller name, it gives me an internal server error but, when everything is "correct", it seems it's unable to find the method in my controller.
So here is my code:
General routing settings
#config/routing.yml
user_routes:
resource: "#AppBundle/Resources/config/user_routes.yml"
type: rest
Bundle routing settings
#AppBundle/Resources/config/user_routes.yml
user:
type: rest
resource: AppBundle\Controller\UserController
And finally my controller:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use AppBundle\Entity\User;
class UserController extends FOSRestController
{
// /**
// * #Rest\Get("/user")
// */
public function getAction()
{
$restresult = $this->getDoctrine()->getRepository('AppBundle:User')->findAll();
if ($restresult === null) {
return new View("there are no users exist", Response::HTTP_NOT_FOUND);
}
return $restresult;
}
}
Using annotation it works well and everybody's happy but when I use these settings I get a 404. I tried adding prefix: /api and name_prefix: api_ according to Symfony official documentation but it didn't work.
I tried also adding a defaults: { _controller: AppBundle:Controller:UserController:get but error 404 was always round the corner.
As I said, if I change the name of the controller class in the user_routes.yml, I get a 500 error so it seems the routing is being read but it's evident that something is missing here and I'm unable to find it neither on the official documentation nor in other places.
Change the name of the action from this:
public function getAction()
to this:
public function getUsersAction()
DOCUMENTATION
Well, just in case it may become useful for someone, I found the solution.
The problem lay in the yml user_routes.yml.
Here is the correct configuration:
path: /user
defaults: { _controller: AppBundle:User:get }
requirements:
_method: GET
That's it ;)

What should be edited in routing.yml in symfony 3 to create a new route from annotation?

I am quite new to Symfony 3. I created the controller files in the AppBundle itself. No new bundle created. Now I am trying to route a new page and it gives an error. This is what I did.
In src\AppBundle\Controller\Patient\PatientController.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("/patient", name="patient_homepage")
*/
public function patientHomeAction(Request $request)
{
// replace this example code with whatever you need
return $this->render('Patient/home.html.twig', array()
);
}
In the app\Resources\views\base.html.twig
Home
This gives an error as
"An exception has been thrown during the rendering of a template
("Unable to generate a URL for the named route "patient_homepage" as
such route does not exist.") in base.html.twig at line 118."
Do I need to do any changes in the app\config\routing.yml also? What am I missing here.
Try this
app:
resource: "#AppBundle/Controller/"
type: annotation
You have to enable route in app/config/routing.yml
app:
resource: "#AppBundle/Controller/Patient/"
type: annotation

How to get route from twig extension class

class AdminExtension extends \Twig_Extension
{
// content...
}
I'm extending twig functionality by adding a class that adds new twig method. Inside this method I want to use current route. How can I do that?
I mean, for example, having:
www.someurl.com/prefix/controller1/aaa/bbb/ccc/ddd
I want to get controller1/aaa/bbb/ccc/ddd part inside the function I described.
Thanks!
You should register the extension as a service and then inject request_stack (as of Symfony 2.4) in the service:
my_extension:
class: ...
arguments: ["#request_stack"]
tags: [{ name: twig.extension }]
Then you can get the request by using RequestStack#getCurrentRequest() and you can get the current url by using Request#getUri().

cant run the controller with symfony 2

I generated my bundle with netbeans with no error , then went to my main config file in the app directory and set this:
first_app:
resource: "#FirstAppBundle/Resources/config/routing.yml"
prefix: /
Then set this in my bundle routing.yml file:
first_app_homepage:
pattern: /hello/{name}
defaults: { _controller: FirstAppBundle:Hello:index }
I have got this controller:
namespace DimaVendor\MytestsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController extends Controller {
public function indexAction($name) {
return new Response('<html><body>' . $name . '</body></html>');
}
}
I get 404 when I go to there:
http://localhost/app.php/hello/Ryan
why?
I also cleaned my cache and found no error
app.php and app_dev.php scripts are the entry points for Symfony apps, and they lie in web/ subfolder in the project - in this case, point your browser to:
http://localhost/PhpProject1/web/app_dev.php/hello/Ryan
.htaccess comes in web/ folder, and redirects any requests to:
http://localhost/PhpProject1/web/some/path
to:
http://localhost/PhpProject1/web/app.php/some/path
in your case. Have a look!

Resources