Symfony 3 routes + VueJS's index.html with F5 falls to 404 - symfony

I try connect Vue.js with Symfony 3, but have no idea how to create proper route configuration for it.
Project tree looks like that:
Project root
- app/
- Resources/
- views/
- default/
- web/
- assets/
- static/
index.html // here Vue is included
From Symfony I provide some REST API, the routes to it are prefixed by /api/. And I have some routes in Vue SPA.
The problem is if I press F5 in browser on any Vue route, which of course is not included in my routing.yml, browser returns 404 error from Symfony.
One half-working decision is when I put content of index.html to Resourses/views/default/twig with such a route:
fallback_for_vue:
path: /{req}
defaults: { _controller: 'AppBundle:Default:index' }
requirements:
req: ".+"
,taken from another question, but this is not actually what I need, because I don't want my frontend programmer to go somewhere except web/ folder.
So which configuration of Symfony routes I should use?

You need a wildcard address to point to your Vue.js application. Otherwise when you reload you send a request to Symfony, and it doesn't know hoy to handle it.
vuejs_wildcard_route:
defaults: { _controller: AppBundle:YourController:yourAction }
path: /vuejsappbasepath/{whatever}
requirements:
whatever: .*
defaults:
whatever: null
I have this setup working with a reactjs app and is working perfectly.
YourController is the controller that contains yourAction, the one that loads the template with the Vue.js panel.

Related

Symfony 3.4. Not any route is working - No route found for "GET /

My Symfony 3.4 project does not recognize any route anymore. Debugging routes in terminal (php bin/console debug:router) does show all the routes but app_dev.php keeps giving error messages. Routing is enabled and set correct in routing.yml file.
Clearing cache also did not work.
mail_chimp:
resource: "#MailChimpBundle/Controller/"
type: annotation
prefix: /
/**
* #Route("/klanten/lijst")
*/
public function klantenLijstAction() {
return;
}
screenshot
Thanks.
> the routing tries to match routes one by one in the order of their definitions; whenever one matches, work is done.
In the routes-list image, you look to have three (maybe more) routes to answer the URL '/'. Which single one should run a controller action?
You should probably set prefixes for the URLs that aren't the index route.

Symfony2 & EmberJS: how to make both prod and dev (/app_dev.php/...) urls working?

Say my application currently responds to the /products route just fine. How do I setup Ember.JS so that /app_dev.php/products would be the same route?
If I understand your problem right, you need to generate Symf route via js instead of static "/products" route?
If so you should use FOSJsRoutingBundle https://github.com/FriendsOfSymfony/FOSJsRoutingBundle
Just set option "expose" to your routing as:
app/config/routing.yml
my_products:
pattern: /products
defaults: { _controller: ProductBundle:Products:index }
options:
expose: true
then you can generate the route depends on env from javascript as:
Routing.generate('my_products')

Symfony2 multiple config and routing files for subdomain routing

We are building a Symfony2 application that will serve different sections using subdomains:
api.tld.com - API system
docs.tld.com - Documentation
assets.tld.com - System for serving images
How we are doing this is creating an app directory for each subdomain, and keeping the standard /app directory in place as the central shared config. There is also a custom bootstrap in the web directory for each app. Subdomains are routed accordingly using .htaccess.
The problem I am having is where multiple config files come in, particularly when they have their own routing imports. In some cases, there can be up to 4 configs.yml files. Take the following URL for example:
http://testing.docs.tld.com
The config setup currently works like this (and it works)
tld.com - Global config located at /app/config/config.yml
testing - Environment config located at /app/config/config_testing.yml. This config also imports config_dev.yml in the same directory.
docs - App config located at /app_docs/config/config.yml
These are all imported in the AppKernal in /app_docs/AppKernal.php:
// Load Global Configuration
// ROUTES INSIDE THIS CONFIG ARE NOT BEING LOADED
$loader->load(__DIR__.'/../app/config/config.yml');
// Load Environment Configuration
// ROUTES INSIDE THIS CONFIG ARE NOT BEING LOADED
$loader->load(__DIR__.'/../app/config/config_' . $this->getEnvironment() . '.yml');
// Load App-centric Configuration
$loader->load(__DIR__.'/config/config.yml');
Now the configs load just fine. But what I'm having trouble with, and not found any definitive documentation on, is when more than one of these configs define framework: router: resources. In the above example configs, these are loaded (attempted to anyway) as follows:
/app/config/config.yml
framework:
secret:%secret%
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: %kernel.debug%
/app/config/config_testing.yml
// No special Routing
/app/config/config_dev.yml
framework:
router: { resource: "%kernel.root_dir%/config/routing_dev.yml" }
/app_docs/config/config.yml
framework:
secret: %secret%
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: %kernel.debug%
All of the configs are loading fine. But what I found is that only the last routing file called above is being included. So I assume the rule is that they are overriden as a rule, rather than extended.
So what I have spent the last couple of days trying to find out is, is it possible to extend the inclusion of routing files within config files in the fashion above? Another option I investigated was to find a way to import routing files in the AppKernal files. I was only able to find this, which doesn't explain exactly at what point this should be used (or where). It doesn't work within the AppKernal where the configs are included, so I assume the Router is not active at that stage.
Anyone have any ideas? I'd be very grateful.
I had the same need so we did like this:
/apps/config
/apps/config/common_config.yml
/apps/config/common_routing.yml
/apps/config/...
/apps/myapp1
/apps/myapp1/myapp1Kernel.php
/apps/myapp1/...
/apps/myapp1/config
/apps/myapp1/config/config.yml
/apps/myapp1/config/routing.yml
/apps/myapp1/config/...
/apps/myapp2
/apps/myapp2/myapp1Kernel.php
/apps/myapp2/...
/apps/myapp2/config
/apps/myapp2/config/config.yml
/apps/myapp2/config/routing.yml
/apps/myapp2/config/...
...
And in each app's yml file, we had:
/apps/myapp1/config/config.yml
imports:
- { resource: "../../config/common_config.yml" }
And then, you have to reproduce the same way in /web
/web/myapp1/app.php
Who will be calling your app
$kernel = new myapp1Kernel('prod', false);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

Symfony2: Can bundle routes be namespaced?

I have a MagazineBundle which in one of its Twig templates has path('portfolio'), the root of a different bundle that has been prefixed.
# app/config/routing.yml
LameMagazineBundle:
resource: "#LameMagazineBundle/Resources/config/routing.yml"
prefix: /
LamePortfolioBundle:
resource: "#LamePortfolioBundle/Resources/config/routing.yml"
prefix: /portfolio
and
# src/Lame/PortfolioBundle/Resources/config/routing.yml
portfolio:
pattern: /
defaults: { _controller: LamePortfolioBundle:Default:index }
But if I add a third bundle, possibly one I've downloaded and installed, and that bundle also happened to also have a route named 'portfolio', would I have to renamed the routes or is there a way to namespace them?
An experiment I tried with two matching route names results in the last declared one overriding the first.
It's not built into the routing system. You would have to manually prefix your route names with the bundle or some other namespace.
The FOSRestBundle has route prefixing functionality but I'm not sure how coupled it is or if it's something that could be easily used without all the bundles other features.
http://symfony.com/doc/master/bundles/FOSRestBundle/6-automatic-route-generation_multiple-restful-controllers.html#naming-collisions

How to display the 404 error page in Symfony2 dev environment

I want to work on the 404 page from the dev environment. I customize 404 using this file : app/Resources/TwigBundle/views/Exception/error.html.twig
This prod url work correctly : mysite.com/404
But this one mysite.com/app_dev.php/404 throw a NotFoundHttpException and give me the dev debug page.
Is it possible to display the error page instead of debug page ?
UPDATE:
The official documentation has now a chapter about that : Testing Error Pages during Development
To display error pages, in web/app_dev.php change second parameter to false
$kernel = new AppKernel('dev', false);
After testing what you need, change it back.
UPDATE
Thanks #user2019515 for pointing that out - now (2.3 and up) there's a link to WebfactoryExeptionsBundle in Symfony docs and the method I wrote above should not be used.
As of Symfony2.6 in dev environment, you can use the following route:
/_error/404.html
Where 404 is the error code to test and html the format of the request.
To be able to use this features, make sure you have the following entry in your routing_dev.yml file:
# app/config/routing_dev.yml
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
You need to override the exception_full.html.twig template on development.
app/Resources/TwigBundle/views/Exception/exception_full.html.twig
Symfony2 uses this template to provide you with as much debugging information as possible during development.
When the kernel is in debug mode, Symfony2 will use exception_full.html.twig, otherwise it will use the specific templates you override.
See vendor/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php, specifically the showAction() and findTemplate() functions for more details.
Also you can add routes to your routing_dev.yml file
error404:
path: /404
defaults:
_controller: FrameworkBundle:Template:template
template: TwigBundle:Exception:error404.html.twig
error500:
path: /500
defaults:
_controller: FrameworkBundle:Template:template
template: TwigBundle:Exception:error500.html.twig

Resources