Symfony : ignore some route - symfony

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'

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.

How do I force a 404 from the symfony routing in YML without specifying a custom controller?

Problem to solve
I want to force to trigger a 404 for a certain specific route in symfony, without having to specify a custom controller that just throws the exception, and before the router continues to explore the rest of the routing file.
Can I say that in the routing.yml file for a specific route?
Context
I have this routing for form submitting in my travel agency:
POST /submit/{submitterForm}
where submitterForm can only take 2 values: purchase-trip and contact depending on if the visitor submits a form in a page where he is visiting a trip or from the contact page. For example POST /submit/contact would be valid.
I have this other route:
GET /{destinationSeoUri}/{tripSeoUri}
to display a specific trip for a destination. For example GET /thailand/trip-bangkok-beaches would be valid.
Limiting values from the router
This is the relevant extract of my routing.yml:
submit:
path: /submit/{submitterForm}
methods: [ POST ]
defaults: { _controller: AppBundle:DataSubmission:submit }
requirements:
submitterForm: purchase-trip|contact
trip:
path: /{destinationSeoUri}/{tripSeoUri}
methods: [ GET ]
defaults: { _controller: AppBundle:Trip:trip }
What I want
For any POST request with an invalid value of {submitterForm} in the route /submit/{submitterForm} I want that the system returns a 404 Not Found
It does not happen now because when the router does not match the submit route pattern, it still falls down to the next ones and then matches the trip pattern.
Then, the system says "okey, I have a route that matches, instead, this is only allowed for GET, so I'll respond a 405 Method not allowed".
This would hold true for something like POST /thailand/trip-bangkok-beaches but I want to specifically signal the client that if the POST matches the /submit/any-invalid-form-submitter-name it is then a Not Found.
Initial solution
To do so, I've inserted, in order, in the middle of the previous two, a catch-all for /submit/* like in this routing excerpt:
submit:
path: /submit/{submitterForm}
methods: [ POST ]
defaults: { _controller: AppBundle:DataSubmission:submit }
requirements:
submitterForm: purchase-trip|contact
submit_fallback:
path: /submit/{submitterForm}
methods: [ POST ]
defaults: { _controller: AppBundle:DataSubmission:notFound }
trip:
path: /{destinationSeoUri}/{tripSeoUri}
methods: [ GET ]
defaults: { _controller: AppBundle:Trip:trip }
This way, requesting to POST /submit/any-invalid-form-submitter-name is matched by the submit_fallback route.
The controller
The controller is a pretty simple "on-liner":
{
throw new NotFoundHttpException();
}
Preferred solution
I think I'm looking for something similar to this:
submit_fallback:
path: /submit/{submitterForm}
methods: [ POST ]
status: 404
So, questions
Is it possible for me to tell the submit_fallback something like: hey guy you don't need a _controller just because you have to send a 404 Not found (or any other status I would want) and nothing else.
If so, how?
Just add requirements onto your other route so that destinationSeoUri cannot equal submit. This should force that route to not match, and throw the 404 instead of the 405 error.
trip:
path: /{destinationSeoUri}/{tripSeoUri}
methods: [ GET ]
defaults: { _controller: AppBundle:Trip:trip }
requirements:
destinationSeoUri: "^(?!submit$)"

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!

How to set correctly my routes files (url) with symfony2.5 with WAMP server

I set my route files in my Symfony2.5 project. But i have this problem when I launched my application with Wamp server (localhost on Windows):
No route found for "GET /" (from "http://localhost/girSymfony/web/")
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »
This is the url used when the application is lauched on WampServer: http://localhost/girSymfony/web/
When I tried with this URL http://localhost/girSymfony/web/I have the 404 error as mentioned above.
But if I try with this url: http://localhost/girSymfony/web/gir/homepage it works; I would like the application arrive and run directly on this url. How can i do that?
This is my Gir/WelcomeBundle/Ressource/config/routing.yml file code:
girWelcomeBundle_HomePage:
pattern: /homepage
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes:  https
And this my app/config/routing.yml file code:
GirWelcomeBundle:
resource: "#GirWelcomeBundle/Resources/config/routing.yml"
prefix: /gir
And here's my controller code for Gir/WelcomeBundle/Controller/HomePageController.php:
<?php
namespace Gir\WelcomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomePageController extends Controller
{
public function indexAction()
{
return $this->render('GirWelcomeBundle:HomePage:index.html.twig');
}
}
Like you can see, I set the routes files, but I just forgot something because I have the 404 error.
I think my application don't use the right direction and the right URL , how can I fix it ?
New edit: Note that I 've set WAMP and Symfony in order when I run my application I arrive directly on the HomePage index
You are on development environemt , you should have to use url like this
Example:
registration:
pattern: /registration
defaults: { _controller: UserBundle:Registration:registration }
http://localhost/girSymfony/web/app_dev.php/registration

Define route without ending slash when importing route in Symfony 2.5?

In file: HardCoreMore/HRAPIBundle/Resources/config/routing.yml
I have imported route like this:
hr_api_company:
resource: "#HRAPIBundle/Resources/config/routing/company.yml"
prefix: /company
And in file: HardCoreMore/HRAPIBundle/Resources/config/routing/company.yml
I have defined route for creating company like this:
hardcoremore_hr_api_company_create:
pattern: /
defaults: { _controller: HRAPIBundle:Company:create }
methods: [POST]
Now the route is matched with following url:
POST company/
but it is not matched when I called it with:
POST company
How can I define route without ending slash when importing route and prefixing it?
this is not possible, see : https://github.com/symfony/symfony/issues/4322
I hope this has helped

Resources