Symfony "No route found" - php

I recently built a new symfony-project with one simple controller to read in a .csv file and output it's content to a template.
I generated the bundle and the controller using the console and gave the controller the route "/browse".
When trying to run, (127.0.0.1:8000/browse) it tells me: "No route found for "GET/browse"".
src/OpiumBundle/Controller/BrowseController.php
<?php
namespace OpiumBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class BrowseController extends Controller {
/**
* #Route("/browse")
*/
public function indexAction() {
$varPath = $this->get('kernel')->getRootDir().'/../var';
return $this->render('OpiumBundle:Browse:index.html.php', array(
// ...
));
}
}
app/config/routing.yml
opium:
resource: "#OpiumBundle/Resources/config/routing.yml"
prefix: /
app:
resource: '#AppBundle/Controller/'
type: annotation
unfortunately I can't post output from my debug:console, because my rep is too low. but there are two empty spaces where I guess they shouldn't:
debug:router
opium_homepage ANY ANY ANY /
homepage ANY ANY ANY /

When using the yml option while generating a bundle, will result in a bundle routing.yml file being created with a Bundle:Default:index of bundle_homepage, and the config file being included as a resource in your app routing.yml file.
Check your src/OpiumBundle/Resources/config/routing.yml file and ensure it reads as.
opium_bundle:
resource: '#OpiumBundle/Controller/'
type: annotation
Alternatively edit your app/config/routing.yml file to read
opium:
resource: "#OpiumBundle/Controller/"
prefix: /
type: annotation
app:
resource: '#AppBundle/Controller/'
type: annotation
Otherwise you will not be able to utilize annotation based routing and would need to manually add the routes to your routing.yml config files.
After making the changes clear your cache
php bin/console cache:clear
Check your routes to ensure that browse is included
php bin/console debug:router
Which should output
opium_browse_index ANY ANY ANY /browse

Related

How to add prefix to some controllers using annotations?

I have at the moment two controllers.
GitlabAuthController
UserController
I need to add the path prefix api to UserController only.
Before I was trying following at annotations.yaml file:
controllers:
resource: ../../src/Controller/
type: annotation
prefix: api
But this adds the prefix to all my controllers.
Is there any way I can add the exception for the GitlabAuthController?
Just create different directories/namespaces for the different types of controllers.
Then you can do:
controllers:
resource: ../../src/Controller/
type: annotation
api_controllers:
resource: ../../src/Controller/Api
type: annotation
prefix: api
Routes defined on the Api namespace would get the /api/ prefix, while the other routes would remain unaffected.
You can check the generated routes are fine by executing bin/console debug:router.

What Does the `type:` Configuration in a Symfony Route Configuration Control?

What does the type: configuration in a Symfony routing file control? What are its valid values?
I can't find this configuration field explicitly documented anywhere. It's referenced indirectly in Symfony's routing documentation.
app_directory:
resource: '../legacy/routing/'
type: directory
and seems related to loading in additional routes. However, its behavior (or all its allowed values) doesn't seem to be explicatly defined anywhere. I can make a guess that it somehow tells Symfony how to load the external routes, but I'd love to know
Is my guess correct?
Are there valid values other than directory or annotation?
Is this formally documented anywhere?
Is there a spot in the Symfony internals that would be a good place to start finding these answers for myself?
You can find how the type works in the Symfony documentation, see code below. It controls if the routes should be loaded from the PHP annotations or the YAML or XML files found in that (bundle) directory.
app_file:
# loads routes from the given routing file stored in some bundle
resource: '#AcmeOtherBundle/Resources/config/routing.yaml'
app_annotations:
# loads routes from the PHP annotations of the controllers found in that directory
resource: '../src/Controller/'
type: annotation
app_directory:
# loads routes from the YAML or XML files found in that directory
resource: '../legacy/routing/'
type: directory
app_bundle:
# loads routes from the YAML or XML files found in some bundle directory
resource: '#AppBundle/Resources/config/routing/public/'
type: directory

Symfony Bundle Load YML Configuration

Since I am new to Symfony and I couldn't manage to find some useful information in google I decided to write to you.
I've read about the way of loading custom DI alias information from a dependency injector in your bundle and how to create a Configuration class that will expose the alias structure. However I am to some extend confused how I can create a file, for example routing.yml, in my AcmeBundle/Resources/config/ folder and read the data from it. E.g:
some_alias:
resource: "#AcmeBundle/Controller/"
type: annotation
prefix: /
I want to make a bundle with routing, independent from the main configuration files in the app folder.
You can create your bundle routing.yml in your WhateverBundle/Resources/config/routing.yml and the in the app/routing.yml just include your bundle's routes.
mybundleorwhatever:
resource: "#WhateverBundle/Resources/config/routing.xml"

FileLoaderLoadException - can not get LexikJWTAuthenticationBundle to work - Symfony

I am trying to build my rest api with symfony and https://github.com/FriendsOfSymfony/FOSRestBundle
I did everything as described in examples but everytime i set the type of my api to "rest" in my routing.yml i get the following error:
[Symfony\Component\Config\Exception\FileLoaderLoadException]
Class could not be determined for Controller identified by
"InveusUserBundle/Controller/UsersController" in
InveusUserBundle/Controller/UsersController
(which is being imported from "/vagrant/app/config/routing.yml").
Make sure there is a loader supporting the "rest" type.
Add route to your controller in: YourBundle/Resources/config/routing.yml
myconfig:
type: rest
prefix: /api
resource: YourBundle\Controller\Api\YourController
name_prefix: api_ # naming collision
Imoprt your bundle routing.yml file into app/config/routing.yml
appRoute:
resource: "#YourBundle/Resources/config/routing.yml"
type: rest
prefix: /api
This config should fix the error.

Symfony2 not picking up multiple routing yml files / Import not working in yml file

So first off TL:DR - Symfony isn't picking up additional routing files in my bundle. Don't know why. Tried doing imports like in config.yml and it's not working either
I have multiple controllers for maintainability of my code. I.e. All site related actions are in a SiteController, all app related actions are in an AppController, etc.
So I figured I'd make routing files to correspond with my controllers. The files are housed in MyBundle/Resources/config. The thing is they are not being picked up when i do a php app/console router:debug. So I thought, well I'll just import them into the routing_mybundle.yml file that symfony generated during the generate:bundle process. So I did the following:
imports:
- { resource: routing_site.yml }
- { resource: routing_app.yml }
I'm getting an error message that says:
routing_mybundle.yml contains unsupported keys for "import": "0", "1". Expected one of: "resource", "type", "prefix", "pattern", "path", "host", "schemes", "methods", "defaults", "requirements", "options".
I realize that it's looking for specific keys, but I'm not sure why it would work in the config.yml but not in a routing.yml file.
If I do the following it works:
imports:
resource: routing_site.yml
Or if I "chain" the imports in the files it works. So by this I mean I import routing_app into routing_site and routing_site into routing_mybundle.
Anyone know how to get the imports tag to work, or how to make it so that symfony will pick up my routing_**.yml files?
Thanks :)
EDIT:
Thanks to forgottenbas for the answer. For those who had the same problem as me (multiple config files in the same bundle) here's what I had to do within the routing.yml file WITHIN myBundle/Resources/config.
My directory structure looks like
MyBundle/
Resources/
config/
routing.yml
routing_site.yml
routing_app.yml
So I had to do the following
SiteController:
resource: routing_site.yml
AppController:
resource: routing_app.yml
Thanks Again
You can import routing files this way
routing.yml
SiteBundle:
resource: "#SiteBundle/Resources/config/routing_site.yml"
AppBundle:
resource: "#AppBundle/Resources/config/routing_app.yml"
FOSUserBundle do it the same.

Categories