Sylius: How to create controller extends from ResourceController? - symfony

I want to create a custom controller:
class ApiappController extends ResourceController
And I added the route:
sylius_api_mobile_user_show:
path: /{id}
methods: [GET]
defaults:
_controller: sylius.controller.apiapp:showAction
But when I call this link, it has always this error message:
Catchable Fatal Error: Argument 1 passed to Sylius\Bundle\ResourceBundle\Controller\ResourceController::__construct() must be an instance of Sylius\Bundle\ResourceBundle\Controller\Configuration, none give
How can I define the config ?
Thank you

You also need to create the configuration for the resource/controller.
sylius_resource:
resources:
acme:
classes:
controller: Acme\FooBundle\Controller\BarController
Your route configuration
acme_foo_bar_index:
path: /
defaults:
_controller: acme.controller.bar:indexAction
_sylius:
template: AcmeFooBundle:Bar:index.html.twig
permission: false
Make note that the controller name is based on name in the resource configuration
The relevant doc page is here: http://docs.sylius.org/en/latest/bundles/general/overriding_controllers.html

Related

In Symfony is it possible to if a route matched, forward to other route

I have two routes in two separated bundles: bundleA_route, bundleB_route. In my /app/config/routing.yml I load them as resources like:
bundle_a_routing:
resource: "#SomeABundle/Resources/config/routing.yml"
prefix: /
bundle_b_routing:
resource: "#SomeBBundle/Resources/config/routing.yml"
prefix: /
In most cases this sequence is required, first of all try to match on routes in bundle_a_routing, and then try to match on routes in bundle_b_routing, but in only one case I want to make an exception, I want a single route defined in bundle_b_routing to be matched before the more "concessive" route defined in bundle_a_routing:
route_in_bundle_a:
path: /admin/{path}
defaults: { _controller: SomeABundle:SomeCtrl:someAction }
methods: [GET]
requirements:
path: ^(.*)$
route_in_bundle_b:
path: /admin/download/{formId}/{fileName}
defaults: { _controller: SomeBBundle:SomeOtherCtrl:someOtherAction }
methods: [GET]
requirements:
formId: \d+
fileName: ^([a-fA-F0-9]{32}(?:\.[a-zA-Z0-9]{1,222})?)$
Now the request URI "http://servername/web/app_dev.php/admin/download/12/23d2fff7f606e93acac9ede5b4e2b394.png" matches the first, but I want to match the second...what is the official scenario in cases like this?
You should change the order in which you are importing routes, so the route http://servername/web/app_dev.php/admin/download/12/23d2fff7f606e93acac9ede5b4e2b394.png will trigger the route_in_bundle_b first and the routes that doesn't have the download segment will not trigger it and will trigger the route_in_bundle_a rule.

Symfony : ignore some route

I want use Symfony4 with Vuejs app in the same corner of my server (on the same domaine)
But Sf routing is master and (than VueJs router) generate some 404
mode: 'history',
using the history mode.
Thanks for your tricks ;) .
approach number 1:
routing configuration to have a "jolly" route:
this route MUST be latest to match
jolly_all_in:
path: '/{jolly}'
controller: 'App\Controller\JollyController::allInPrintVueInit'
requirements:
jolly: '.*'
or in this case all your vue route MUST has a prefix "your-vue-route-prefix"
jolly_all_in_with_prefix:
path: '/your-vue-route-prefix/{jolly}'
controller: 'App\Controller\JollyController::allInPrintVueInit'
requirements:
jolly: '.*'
with this approach you must manage your 404 from JS.
approach number 2:
you MUST map all your JS route to symfony route.
example
user_list:
path: '/user'
controller: 'App\Controller\VueController::printVueInit'
user_get:
path: '/user/{id}'
controller: 'App\Controller\VueController::printVueInit'
requirements:
id: '\d+'
[...]:
path: '/.../.../...'
controller: 'App\Controller\VueController::printVueInit'

Symfony: Controller\AdvertController::viewSlugAction()" requires that you provide a value for the "$format" argument

I am following the Symfony openclassrooms tutorial. I am currently at the chapter "routes avancées". I also posted my question there.
I am trying to open this local URL:
http://localhost/Symfony/web/app_dev.php/platform/2011/webmaster
or
the same url with the html extension
But I keep getting this error message:
My resources/config/routing.yml is as follows:
I am not sure about the path in neo_platform_home. I tried both, with and without the slash.
neo_platform_home:
path: /
defaults: {_controller: NeoPlatformBundle:Advert:index}
neo_platform_view:
path: /advert/{id}
defaults: {_controller: NeoPlatformBundle:Advert:view}
neo_platform_view_slug:
path: /{year}/{slug}.{_format}
defaults:
_controller: NeoPlatformBundle:Advert:viewSlug
_format: html
requirements:
year: \d{4}
format: html|xml
neo_platform_add:
path: /add
defaults: {_controller: NeoPlatformBundle:Adert:add}
The /app/config/routing.yml is as folows:
neo_platform:
resource: "#NeoPlatformBundle/Resources/config/routing.yml"
prefix: /platform
When I try to open the url with the .xml extension, I get this error message:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
at vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ArgumentResolver.php:78
...
difficult to answer without your code, but it look like your forgrgot to pass the $format to your viewSlugAction controller

Optional Prefix part in Route

I'm trying to create a rather complex-route schema and I'm stuck.
Routes that I wan't to match
/books indexAction
/books/show showAction
/books/authorname indexAction
/books/authorname/show showAction
Current Setup
Routing.yml
actions:
resource: routing/actions.yml
prefix: /books/{authorname}
requirements:
authorname: ".*"
defaults:
authorname: ''
routing/actions.yml
books_index:
path: ""
defaults: { _controller:bookController:indexAction }
books_show:
path: "/show"
defaults: { _controller:bookController:showAction }
This Setup currently matches only:
/books/ indexAction
/books/show showAction
/books/authorname indexAction
/books/authorname/show showAction
but not
/books
The reasons why I split those routes into two files is that in reality the actions contain much more routes, also there is other routing-categories then the actions.
I know I could define the /books route manually without a prefix but I want to avoid this as this schema will be used for many different modules.
You need a /book prefix in any case.
One solution is to define separate entries in your routing.yml for that. One with /books prefix and the second with /books/{authorname}.
index:
resource: routing/actions.yml
prefix: /books
show:
resource: routing/actions.yml
prefix: /books/{authorname}
It's not so elegant, but in this case you can get rid of extra requirements section.
Hope this helps!

Symfony2 router - how to call annotation route

Simple case. In my all app I'm using Route annotation driver, defined in routing.yml as follows:
MyBundle:
resource: "#MyBundle/Controller/"
type: annotation
prefix: /someprefix
Some action in MyBundle's Ctrl controller looks like:
#Route("/{page}/{status}", name="default_action", defaults={"page" = 1, "status" = "ok"}, requirements={"page" = "\d+"})
public function defaultAction($page, $status) {...}
Now I want to make this action - default action when visiting my web page. I cannot use just #Route("/") because I'm prefixed. So I'm adding to routing.yml:
_welcome:
pattern: /
defaults: { _controller: MyBundle:Ctrl:default }
And there is where problem starts - Symfony2 is calling default controllers action not from annotation but just from action and I get error:
Controller "...:defaultAction()" requires that you provide a value for the "$page" argument (because there is no default value or because there is a non optional argument after this one).
So simply Symfony2 is not obtaining default values from #Route annotation.
Question is: how to call route for _welcome that is aware of #Route?
You've missed defaults in yml settings, should look like this:
_welcome:
pattern: /
defaults: { _controller: MyBundle:Ctrl:default, page: 1, status: ok }

Resources