I tried to select a controller by host.
This is what I did in my routing.yml:
rout_default:
resource: "#DefaultBundle/Controller/"
type: annotation
prefix: /
rout1:
resource: "#DefaultBundle/Controller/Controller1Controller.php"
type: annotation
prefix: /
host: example.com
rout2:
resource: "#DefaultBundle/Controller/Controller2Controller.php"
type: annotation
prefix: /
host: example2.com
In the contollers I have following Actions:
/**
* #Route("/data", name="data")
*/
public function dataAction()
{
//...
}
I have these functions in both controllers. I know that the action and the name is the same, but I hoped, that the system will notice the difference by the host.
If I type debug:router in the console, there is only the route of rout2. The other one is not visible.
Thank you for your help.
Related
Why I am getting errors for the route?
Other APIs for the CRUD operation are working fine.
I have added a new controller code for my new API and I am getting a route error.
What could be the mistake?
While I have already written my route in routing.yml
creat:
path: /create
defaults: { _controller: AcmeDemoBundle:DemoController:create }
methods: [GET]
Products:
path: /recentProducts/{id}
defaults: { _controller: AcmeDemoBundle:DemoController:recentProducts }
methods: [GET]
Update:
path: /updateProduct/{id}
defaults: { _controller: AcmeDemoBundle:DemoController:update }
methods: [POST]
Delete:
path: /deleteProduct/{id}
defaults: { _controller: AcmeDemoBundle:DemoController:delete }
methods: [DELETE]
referenceCreation:
path: /koco/create
defaults: { _controller: AcmeDemoBundle:DemoController:createReference }
methods: [GET]
My api is
http://localhost/koco1/web/app_dev.php/demo/koco/create
My Controller is
/**
* #Route("/koco/create", name="referenceCreation")
* #Template()
*/
public function createReferenceAction()
{
$organization = new Organization();
$organization->setName('Sensio Lab');
$user = new User();
$user->setName("Jonathan H. Wage");
$user->setOrganization($organization);
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($organization);
$dm->persist($user);
$dm->flush();
return new Response('Created product id ');
// $response = new Response('Hello Huzaifa ');
// return $response;
}
What is wrong here?
Kindly let me know.
Thanks
Your routing is off.
Also you are creating routes through yaml and through annotations. Should stick to one.
Also server setup looks off too, http://localhost/koco1/web/app_dev.php/demo/koco/create should be more like http://localhost/koco1/web/koco/create, you can try running php server. Going to /public and running php -S localhost:8000 you can read more here: https://www.php.net/manual/en/features.commandline.webserver.php
or symfony cli server https://symfony.com/download
Your routes does not match /demo/koco/create is not equal to /koco/create, you should first of all set up correctly, then it will be easier to debug.
I created a Symfony 4.4.8 project with translations to English and French languages, so I followed the steps:
https://symfony.com/doc/current/translation.html
and set:
config/packages/translation.yaml
framework:
default_locale: 'fr'
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks: ['en', 'fr']
config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
prefix: /{_locale}/
requirements:
_locale: fr|en
and a src/Controller/HomeController.php with
class HomeController extends AbstractController
{
private $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* #Route("/", name="home")
*/
public function index() {
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
]);
}
I have the translation files in translations folder and when I run localhost:8000/fr or localhost:8000/en, it works fine, my home/index.html.twig is shown.
The issue is when I run localhost:8000/, it shows me the default Welcome to Symfony page with "You're seeing this page because you haven't configured any homepage URL."
Is this solution for sf2 will work on sf4?
Is anybody know how to solve it ?
I tested also to change the route in HomeController to:
#Route("/{_locale}", name="home", defaults={"_locale"="fr"}
but it returns exception:
Route pattern "/{_locale}/{_locale}" cannot reference variable name "_locale" more than once.
workaround, added:
RedirectMatch ^/$ /fr
in my virtualhost (with apache2 server)
pure symfony solution shoud be better !
I would guess a bad routing configuration. I couldn't recall a default behaviour where we can set the locale by adding the locale code in the URL so I would guess it is something your added ;)
Symfony resolves the route in the same order as you declare the route. So one solution would be to import 2 times the same route but with the prefix:
# config/route.yaml
app_front:
type: annotation
resource: '../src/Controller/*.php'
prefix: /
options:
expose: true
app_localized_front:
type: annotation
resource: '../src/Controller/*.php'
name_prefix: localized_
prefix: /{locale}
options:
expose: true
I know the previous config works, but I don't know if this is the more elegant way to do it :)
I'm currently stuck with routing in my Symfony4 (4.3) project. My problem is pretty simple, I want to use route annotations in my controllers but I want to defined the order of these one.
For example, if I have two controllers with following routing :
class BarController extends AbstractController
{
/**
* #Route("/test/{data}", name="app_bar")
*/
public function index($data)
{
// ...
return $this->render('index.html.twig', [
'data' => $data,
]);
}
}
and
class FooController extends AbstractController
{
/**
* #Route("/test/my_value", name="app_foo")
*/
public function index()
{
// ...
return $this->render('index.html.twig', [
'data' => 'my_value',
]);
}
}
In config/routes/annotations.yaml I define my route like this
app_controllers:
resource: ../../src/Controller/
type: annotation
Then if I call /test/my_value I would like to be redirected to FooController since his index action define #Route("/test/my_value", name="app_foo") but like routes is loaded alphabetically the index action from BarController with app_bar route is called first.
So I have tried to defined following routing :
app_foo_controller:
resource: ../../src/Controller/FooController.php
type: annotation
app_controllers:
resource: ../../src/Controller/
type: annotation
But this didn't work, BarController and his app_bar route still called before app_foo route from FooController.
Also, I don't understand the purpose of config/routes/annotations.yaml vs config/routes.yaml since both could contains any type of routes... I miss something ?
Nevermind I found the solution. I just miss the fact that I override my specifically app_foo_controller routing when I define app_controllers the solution is to defined each controllers like that :
app_controllers:
resource: ../../src/Controller/
type: annotation
app_foo_controller:
resource: ../../src/Controller/FooController.php
type: annotation
app_bar_controller:
resource: ../../src/Controller/BarController.php
type: annotation
I got defined routes in routing.yml file
one route is:
Profile_user_profile:
path: /profile/{id}
defaults: { _controller: ProfileBundle:Users:profile }
methods: [get]
and second is:
Profile_accept_connection_proposal:
path: /profile/acceptProposal
defaults: { _controller:ProfileBundle:Users:acceptConnectionProposal }
methods: [put]
First route without methods: [get] listen also and [put] request and catch second url before it get to route definition. Is there way to define checking for parameter only if url is numeric.
Just add the requirements parameter to accept only digits for a determined route like this:
Profile_user_profile:
path: /profile/{id}
defaults: { _controller: ProfileBundle:Users:profile }
methods: [get]
requirements: <--- ADDED PARAMETER
id: \d+
For more infos read the Symfony book about Routing. There you can find more advanced example on how to use route parameters.
You can now do this with Annotations in your controller like so:
class UserController extends AbstractController
{
/**
* #Route("/profile/{id}", name="user_profile", requirements={"id"="\d+"})
*/
public function profile($id)
{
// ...
}
}
More info on Symfony's docs
Specifically defining routing requirements
I made a command that automatically writes in routing.yml.
My problem is that when I try to browse one of the routes
api:
resource: "."
type: "api"
prefix: /api
I get this exception
Cannot load resource "."
I tried to add a cache:clear to my command but I get the same exception.
I added a cache warmup that runs after the command termination that way Symfony dumps routes into generated code .
class TerminateListener {
public function onConsoleTerminate(ConsoleTerminateEvent $event) {
if ($event->getCommand()->getName() == ('my:command')) {
$app = new Application();
$cache_clear_command = $event->getCommand()->getApplication()->find('cache:warmup');
$cache_clear_command->setApplication($app);
$event->getOutput()->setVerbosity('VERBOSITY_QUIET');
$cache_clear_command->run($event->getInput(), $event->getOutput());
}
}
}
services:
warmup.listener:
class:TerminateListener
tags:
- { name: kernel.event_listener, event: console.terminate , method: onConsoleTerminate }