How to fix "Circular reference" error on YAML file? - php

FileLoaderImportCircularReferenceException: Circular reference
detected in "/app/config/routing_dev.yml"
("/app/config/routing_dev.yml" >
"/app/config/routing.yml" > "." >
"#GabrielAdminPanelBundle/Controller/" >
"/app/config/routing_dev.yml").
I'm trying to achieve this:
http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html#more-advanced-loaders
so I created this file
AdvancedLoader.php
<?php
//namespace Acme\DemoBundle\Routing;
namespace Gabriel\AdminPanelBundle\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\RouteCollection;
class AdvancedLoader extends Loader
{
public function load($resource, $type = null)
{
$collection = new RouteCollection();
$resource = '#GabrielAdminPanelBundle/Resources/config/routing.yml';
$type = 'yaml';
$importedRoutes = $this->import($resource, $type);
$collection->addCollection($importedRoutes);
return $collection;
}
public function supports($resource, $type = null)
{
return $type === 'advanced_extra';
}
}
/src/Gabriel/AdminPanelBundle/Resources/config/services.yml
services:
gabriel.routing_loader:
class: Gabriel\AdminPanelBundle\Routing\AdvancedLoader
tags:
- { name: routing.loader }
/app/config/routing.yml
gabriel_messaging:
resource: "#GabrielMessagingBundle/Controller/"
type: annotation
prefix: /
fos_js_routing:
resource: "#FOSJsRoutingBundle/Resources/config/routing/routing.xml"
# app/config/routing.yml
Gabriel_Extra:
resource: .
type: advanced_extra
app/config/routing_dev.yml
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_configurator:
resource: "#SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix: /_configurator
_main:
resource: routing.yml
/src/Gabriel/AdminPanelBundle/Resources/config/routing.yml
gabriel_admin_panel:
resource: "#GabrielAdminPanelBundle/Controller/"
type: advanced_extra
prefix: /superuser

Take a close look at #GabrielAdminPanelBundle/Resources/config/routing.yml:
gabriel_admin_panel:
resource: "#GabrielAdminPanelBundle/Controller/"
type: advanced_extra
prefix: /superuser
The type specifies which loader should be used, in this case you said advanced_extra, which is your loader. Your loader includes this file again and the file will execute the loader again, this will continue forever (in other words: a circular reference).
Please also note that you already included the routes in app/config/routing.yml:
gabriel_messaging:
resource: "#GabrielMessagingBundle/Controller/"
type: annotation
prefix: /
This time, you use the correct type: annotation. You should remove this entry and edit the #GabrielAdminPanelBundle/Resources/config/routing.yml file to use correct types.

Related

Order of Route in Symfony

I Use Symfony 3, And I Want to understand How symfony chouses the Route.
I have 3 routes, 2 in routing.yml and 1 in annotation.
this is my code:
app/config/routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
hello:
path: "hello/{firstName}"
defaults: {_controller: AppBundle:Default:rotta,firstName: "route1"}
hello_name:
path: "hello/{firstName}"
defaults: {_controller: AppBundle:Default:rotta, firstName: "route2"}
src/AppBundle/Controller/DefaultController.php
/**
* #Route("hello/{firstName}", name="hello", defaults={"firstName" = "Annotation"})
*/
public function rottaAction($firstName,Request $request)
{
var_dump($request->get('firstName'));
exit;
}
My result is
string 'route1' (length=6)
with this routing.yml
hello:
path: "hello/{firstName}"
defaults: {_controller: AppBundle:Default:rotta,firstName: "route1"}
hello_name:
path: "hello/{firstName}"
defaults: {_controller: AppBundle:Default:rotta, firstName: "route2"}
app:
resource: "#AppBundle/Controller/"
type: annotation
My result is
string 'route2' (length=6)
and with this combination:
hello:
path: "hello/{firstName}"
defaults: {_controller: AppBundle:Default:rotta,firstName: "route1"}
hello_name:
path: "hello/{firstName}"
defaults: {_controller: AppBundle:Default:rotta, firstName: "route2"}
#app:
# resource: "#AppBundle/Controller/"
# type: annotation
my result is:
string 'route1' (length=6)

How to register yaml routes as PHP?

First I added a new routing_dev file as php
framework:
router:
# resource: "%kernel.root_dir%/config/routing_dev.yml"
resource: "%kernel.root_dir%/config/routing_dev.php"
strict_requirements: true
profiler: { only_exceptions: false }
Now I'm registering the routes according to the
http://symfony.com/doc/current/book/routing.html
However I'm having an issue, when I register a yaml route example:
gabriel_auth:
resource: "#GabrielAuthBundle/Controller/"
type: annotation
prefix: /
gabriel_messaging:
resource: "#GabrielMessagingBundle/Controller/"
type: annotation
prefix: /
like this on the file
$collection->addCollection(
$loader->import("#GabrielAuthBundle/Controller/", "annotation")
);
$collection->addCollection(
$loader->import("#GabrielMessagingBundle/Controller/", "annotation")
);
then it does work, however
I can't find a way to add these type of resources to the collection:
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
this is my best try
$collection->addCollection(
$loader->import(
'#WebProfilerBundle/Resources/config/routing/profiler.xml')->addPrefix('/_profiler')
);
$collection->addCollection(
$loader->import(
'#WebProfilerBundle/Resources/config/routing/wdt.xml')->addPrefix('/_wdt')
);
it throws this error
FileLoaderLoadException: Catchable Fatal Error: Argument 1 passed to
Symfony\Component\Routing\RouteCollection::addCollection() must be an
instance of Symfony\Component\Routing\RouteCollection, null given
According to the doc http://api.symfony.com/2.7/Symfony/Component/Routing/RouteCollection.html#method_addPrefix
public addPrefix(string $prefix, array $defaults = array(), array $requirements = array())
Adds a prefix to the path of all child routes.
Parameters
string $prefix An optional prefix to add before each pattern of the route collection
array $defaults An array of default values
array $requirements An array of requirements
This function has no return
Thats's why you got null
Try:
$profilerCollection = $loader->import( '#WebProfilerBundle/Resources/config/routing/profiler.xml');
$profilerCollection->addPrefix('/_profiler');
$collection->addCollection( $profilerCollection);

Symfony2 Circular reference in my routing.dev.yml

I am trying to build a login and registration forms and processes in my project, but I am getting the circular reference error for some reason. The registration works fine. The problem is when I am building a login form. I am using the tutorial from here:
http://symfony.com/doc/current/cookbook/security/form_login_setup.html.
When I add this the project still works:
firewalls:
default:
anonymous: ~
http_basic: ~
form_login:
login_path: /login_user
check_path: /login_check
But when I make the routes in the controller I get the error:
<?php
// src/AppBundle/Controller/SecurityController.php
// ...
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class SecurityController extends Controller
{
/**
* #Route("/login_user", name="login_route")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'Frontend/navbar.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
/**
* #Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
// this controller will not be executed,
// as the route is handled by the Security system
}
}
This is my routing.dev.yml:
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_configurator:
resource: "#SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix: /_configurator
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
_main:
resource: routing.yml
What can be the problem here?

Why is symfony2 functional test client->request() not getting successful response()?

So I've decided to delve into phpunit testing and I've shamefully written out my php code before writing my test. Anyway, I'm just writing a very simple test that tells me if I actually found the correct web page. Unfortunately my one assertion test keeps failing. I know my route "/login" is correct because when I navigate to localhost/index.php/login (where index.php is a link to app_dev.php), the page comes up correctly. Bellow is my routing.php file:
caremonk_mainsite_login:
pattern: /login
defaults: { _controller: CaremonkMainSiteBundle:Security:login }
requirements:
_method: POST|GET
caremonk_mainsite_login_check:
pattern: /login_check
requirements:
_method: POST|GET
caremonk_mainsite_signup:
pattern: /signup
defaults: { _controller: CaremonkMainSiteBundle:CreateUser:signup }
requirements:
_method: POST|GET
caremonk_mainsite_logout:
pattern: /logout
defaults: { _controller: CaremonkMainSiteBundle:Security:logout}
requirements:
_method: POST|GET
caremonk_mainsite_post_blog:
pattern: /post_blog
defaults: { _controller: CaremonkMainSiteBundle:UserEvents:post }
requirements:
_method: POST|GET
caremonk_mainsite_my_profile:
pattern: /my_profile_edit
defaults: { _controller: CaremonkMainSiteBundle:UserEvents:editProfile }
requirements:
_method: POST|GET
caremonk_mainsite_activate:
pattern: /activate/{username}/{token}
defaults: { _controller: CaremonkMainSiteBundle:CreateUser:activateAccount }
requirements:
_methods: GET
caremonk_mainsite_password_reset_request:
pattern: /reset_password/
defaults: { _controller: CaremonkMainSiteBundle:Security:passwordResetRequest }
requirements:
_methods: GET | POST
caremonk_mainsite_reset_password_email:
pattern: /reset_password_email/{username}/{resetPasswordToken}
defaults: { _controller: CaremonkMainSiteBundle:Security:sendNewPassword }
requirements:
_methods: GET
caremonk_mainsite_change_password:
pattern: /change_password
defaults: { _controller: CaremonkMainSiteBundle:Security:changePassword }
requirements:
_methods: GET | POST
caremonk_mainsite_home:
pattern: /
defaults: { _controller: CaremonkMainSiteBundle:Home:index }
requirements:
_methods: GET
Anyway bellow is the test code that keeps failing:
<?php
namespace Caremonk\MainSiteBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SecurityControllerFunctionalTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
// I've done many tests
// I've tried the following request with all failed results
// $crawler = $client->request('GET', 'index.php/login');
// $crawler = $client->request('GET', 'http://localhost/indpex.php/login');
// $crawler = $client->request('GET', 'localhost/index.php/login');
// You get the idea
$crawler = $client->request('GET', '/login');
$this->assertTrue($client->getResponse()->isSuccessful());
}
}
My routing.yml and routing_dev.yml files are shown bellow
#routing_dev.yml
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_configurator:
resource: "#SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix: /_configurator
_main:
resource: routing.yml
#routing.yml
caremonk_main_site:
resource: "#CaremonkMainSiteBundle/Resources/config/routing.yml"
prefix: /
You prefixed your imported routes with a "/", and your routes path start with a "/".
Normally I would prefix my routes with something more meaningful (and does not end with a "/") or remove the "/" from your imported routes.
Running the following command should give you insight to how your routes are registered.
app/console debug:router

How to route in symfony2?

Well, having the following in my routing_dev.yml
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
homepage:
pattern: /
defaults: { _controller: AcmeDemoBundle:Ideup:index }
aboutme:
pattern: /aboutme
defaults: { _controller: AcmeDemoBundle:Ideup:about }
_ideup:
resource: "#AcmeDemoBundle/Controller/IdeupController.php"
type: annotation
prefix: /ideup
_form:
resource: "#AcmeDemoBundle/Controller/FormController.php"
type: annotation
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_main:
resource: routing.yml
I'm trying to have a simple form:
<form action="{{ path('_form') }}" method="POST" id="contact_form">
But I keep getting this following exception: "Route "_form" does not exist."
I just don't know where else to look and what to do, any ideas?
_form:
resource: "#AcmeDemoBundle/Controller/FormController.php"
type: annotation
This will just load annotated routes from the FormController.
For example:
/**
* #Route("/my/route", name="my_route")
*/
public function myRouteAction() { ... }
If you want a _form route, you should either load all annotationed routes from the FormController or define it in another way:
_form:
pattern: /path/to/your/form
defaults: { _controller: AcmeDemoBundle:Form:myFormAction }
Of course myFormAction() should exist in the FormController.

Categories