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.
Related
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.
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
I have a very strange issue, with my Symfony2 setup.
I'm working on a restful webservice and would like to setup routing.
I have a fully working application and woud like to change my routing.yml config.
Working configuration
my_product:
resource: My\Bundle\ProductBundle\Controller\DefaultController
type: rest
prefix: /
When I change that to:
my_product:
resource: "#MyProductBundle/Controller/"
type: rest
prefix: /
I get the following error:
Symfony\Component\Config\Exception\FileLoaderLoadException"
message="Can't find class for controller
"#MyProductBundle/Controller/" in #MyProductBundle/Controller/ (which
is being imported from
"/home/myproduct/domains/example/v5/app/config/routing.yml"). Make
sure the "MyProductBundle" bundle is correctly registered and loaded
in the application kernel class. If the bundle is registered, make
sure the bundle path "#MyProductBundle/Controller/" is not empty.
When I change the type from "rest" to "annotation", the error disappears.
What am I doing wrong? I can't find it out and my searches lead to nothing.
Many thanks in advance!
You can't currently import all of a bundle's controllers at once when using FOSRestBundle. It will be added in FOSRestBundle 2.0. Import your controllers individually like in your first example.
I have issue with routing in my Symfony 2 application.
This application contains 2 bundles MainSiteBundle and GalleryBundle
I configured routing with prefixes like that:
app/config/routing.yml
honorata_photo_main_site:
resource: "#HonorataPhotoMainSiteBundle/Resources/config/routing.yml"
prefix: /
honorata_photo_gallery:
resource: "#HonorataPhotoGalleryBundle/Resources/config/routing.yml"
prefix: /gallery
Routine inside each bundle is not important now because i have issue with this.
When I try to access / route everything works fine (even with sub routes inside bundle)
When I try to access /gallery route it shows me error like:
No route found for "GET ery"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
Why Symfony 2 router cuts first 3 letters after / ?
I would guess that inside "#HonorataPhotoMainSiteBundle/Resources/config/routing.yml" you have some more general route defined.
Try reversing the order of definition:
honorata_photo_gallery:
resource: "#HonorataPhotoGalleryBundle/Resources/config/routing.yml"
prefix: /gallery
honorata_photo_main_site:
resource: "#HonorataPhotoMainSiteBundle/Resources/config/routing.yml"
prefix: /
Subject resolved and closed !
Problem was that /gallery path already exist in MainSiteBundle I realised that using php app/console route:debug and after deeper analysis :)
Next time I will make routing log to prevent that in the future.
I followed this tutorial and got SonataAdminBundle successfully installed. However, I'm very new to Symfony2 and I'm having trouble understanding parts of the routing configuration found in the tutorial.
# app/config/routing.yml
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
Where does resource: . get the routes from?
What does type: sonata_admin mean?
Thanks!
The type key permits to the different routing loaders to know which one supports loading this routing resource.
The resource: . is just here to say that it will load everything. In fact, the routing loader does not care about this parameter, that's why you put ..