I got a hard time learning Symfony 2.8.
I have created a bundle called BlogBudle and inside this I created a controller called HomeController.
My goal is:
Create a /test URL and assign it to home page url
Below is my code:
/var/www/symfony/app/config/routing.yml
blog:
resource: "#BlogBundle/Controller/"
type: annotation
prefix: /
app:
resource: "#AppBundle/Controller/"
type: annotation
/var/www/symfony/src/BlogBundle/Controller/HomeController.php
namespace BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class HomeController extends Controller
{
/**
* #Route("/test")
*/
public function indexAction()
{
return $this->render('BlogBundle:Default:index.html.twig');
}
}
When I run php app/console debug:router, it shows
Name Method Scheme Host Path
blog_home_index ANY ANY ANY /test
Problem:
If I visit http://www.example.com/test, it's showing 404 error.
Also below is my VH Configuration:
<VirtualHost *:80>
#ServerAdmin admin#test.com
ServerName mysmfony.com
ServerAlias www.mysymfony.com
DocumentRoot /var/www/symfony/web
DirectoryIndex app_dev.php
<Directory /var/www/symfony/web>
AllowOverride All
Options All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/mysymfony.com.log
CustomLog ${APACHE_LOG_DIR}/mysymfony.com.log combined
</VirtualHost>
Need to overwrite your bundle route in app/config/routing.yml such as :
user_test:
resource: "#BlogBundle/Resources/config/routing.yml"
prefix: /
and
Here is route for redirect to your Home page
BlogBundle/Resources/config/routing.yml
any_unique_route_name:
pattern: /test
defaults: { _controller: BlogBundle:Home:index }
Make sure this is helpful to you.
Here is an example :
routing.yml
my_route_name:
path: /test
defaults:
_controller: NamespaceBlogBundle:Home:index
Also if your controller is called HomeController render should be like this
public function indexAction()
{
return $this->render('NamespaceBlogBundle:Home:index.html.twig');
}
Symfony routing doc
Related
I developped a site with sympfony/phpp i want to deploy on a local server.
After the development on my own computer (where it works well), I put the project on a local server with apache, php 7.3.x, composer 2.0.0 under a debian 10 OS
All the transfer of the project from my computer to the server and the setting are well finished.
The project folder name is si2a. It is in the directory /var/www
I set the permissions access on this directory with ACL following a symfony doc guideline.
I set the site configuration file those the name is si2afama.conf. This last is in the directory /etc/apache2/sites-available. After setting, I activate the site, reload apache and clear the cache. Here is the si2afama.conf's contain :
<VirtualHost *:80>
ServerName si2afama
# ServerAlias www.si2afama.com
DocumentRoot /var/www/si2a/public
DirectoryIndex /index.php
<Directory /var/www/si2a/public>
AllowOverride All
Require all granted
Allow from All
FallbackResource /index.php
</Directory>
<Directory /var/www/si2a/public/bundles>
FallbackResource disabled
</Directory>
ErrorLog /var/log/apache2/si2a_error.log
CustomLog /var/log/apache2/si2a_access.log combined
</VirtualHost>
When i try to access the site, i found this error :
No route found for "GET /si2afama"
Since, it is a Routing issus, here are some files's contain :
si2a/config/packages/routing.yaml
framework:
router:
utf8: true
si2a/config.routes.yaml
index:
path: /
controller: App\Controller\DefaultController::index
si2a/public/index.php.
<?php
use App\Kernel;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpFoundation\Request;
require dirname(__DIR__).'/config/bootstrap.php';
if ($_SERVER['APP_DEBUG']) {
umask(0000);
Debug::enable();
}
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts([$trustedHosts]);
}
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
I don't really know what to do in order to fix the issus.
May someone help me, please. Thank you.
Your vhost is related to your domain name and directory of source code,
if you want a route name like si2afama for your application, you have to add it to symfony routes.
# si2a/config.routes.yaml
index:
path: /
controller: App\Controller\DefaultController::index
si2afama:
path: /si2afama
controller: App\Controller\DefaultController::nameActionFromController
try to install this package composer require symfony/apache-pack it will create a .htaccess file that'll fix your problem
in my Symfony 5 project, I am looking to use different subdomains for my controllers depending on the environment I am in (dev, prod).
For this, I created:
config/routes/dev/annotations.yaml
and
config/routes/prod/annotations.yaml
Each file contains its own host for my controllers.
config/routes/dev/annotations.yaml:
blog_controllers:
resource: ../../../src/Controller/BlogController.php
type: annotation
host: 'blog.{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%'
app_controllers:
resource: ../../../src/Controller/HomeController.php
type: annotation
host: 'app.{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%|www.%domain%'
config/routes/prod/annotations.yaml:
blog_controllers:
resource: ../../../src/Controller/BlogController.php
type: annotation
host: 'prod.blog.{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%'
The %domain% is the APP_DOMAIN in my .env, no problem with that.
So, when I'm on prod environment, when I'm trying to access https://prod.blog.[myDomain] (the homepage on my blog project part), I'm redirected on the homepage of the HomeController. I don't know why...
My controllers :
BlogController.php:
class BlogController extends AbstractController
{
/**
* #Route("/", name="blog")
*/
public function index()
{
return $this->render('blog/index.html.twig');
}
}
HomeController.php:
class HomeController extends AbstractController
{
/**
* #Route("/", name="home")
*/
public function index()
{
return $this->render('home/index.html.twig');
}
}
edit:
I tested with this configuration :
Just in prod, set this route in the /config/prod/annotations.yaml
blog_controllers:
resource: ../../../src/Controller/BlogController.php
type: annotation
host: 'blog.{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%'
No host for dev environment.
So, on prod environment, if I make a debug:router, I've :
[![enter image description here][1]][1]
So both routes here are correct.
On the one hand, the "home" route without a subdomain, accessible by http s://myDomain.com
and on the other, the "blog" route accessible by http s://blog.myDomain.com
Are we OK?
But when I launch http://blog.myDomain.com/, It's the Home controller which is displayed !
EDIT 2 : Okay I found the solution.
Just add host for "home" route, like this :
app_controllers:
resource: ../../../src/Controller/HomeController.php
type: annotation
host: '{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%|www.%domain%'
No subdomain. It works.
Thanks all !
[1]: https://i.stack.imgur.com/GPJlN.png
I am trying to route a subdomain to a specific bundle in Symfony2. Here's what I've got:
I added local domains to my hosts:
127.0.0.1 todolist.lc
127.0.0.1 manager.todolist.lc
I created a virtual host that forwards all subdomains to my Symfony installation:
<VirtualHost 127.0.0.1>
ServerName todolist.lc
ServerAlias *.todolist.lc
DirectoryIndex app_dev.php
DocumentRoot "C:\xampp\htdocs\todolist\web"
</VirtualHost>
I created a new Bundle to handle the subdomain manager.todolist.lc:
Now I am trying to set up the route to manager.todolist.lc:
frontend:
resource: "#FrontendBundle/Controller/"
type: annotation
prefix: /
backend:
resource: "#BackendBundle/Controller/"
type: annotation
prefix: /api
manager:
host: manager.todolist.lc
resource: "#ManagerBundle/Controller/"
type: annotation
prefix: /
Now after I added the manager route I get a FileLoaderImportCircularReferenceException on every route there is.
I also tried to use a prefix, but this also gives the Exception:
manager:
resource: "#ManagerBundle/Controller/"
type: annotation
prefix: /manager
I can't figure out what I am missing. What am I doing wrong? If you need any more info, just ask for it in the comments and I'll provide it.
Ok. Here's what I was missing:
1. I forgot to load the bundle into the AppKernel
Obviously, this is very important:
new FrontendBundle\FrontendBundle(),
new BackendBundle\BackendBundle(),
new ManagerBundle\ManagerBundle(),
2. The subdomain needs to be declared before the main domain
After I loaded the bundle into the AppKernel the application would still route to the FrontController. I solved this by changing the order of my routes:
manager:
host: manager.todolist.lc
resource: "#ManagerBundle/Controller/"
type: annotation
prefix: /
frontend:
resource: "#FrontendBundle/Controller/"
type: annotation
prefix: /
backend:
resource: "#BackendBundle/Controller/"
type: annotation
prefix: /api
After changing the order of the routes both manager.todolist.lc and todolist.lc worked.
I have just installed Symfony2 and created my bundle. I removed the Acme demo bundle and tested my new route "/" which worked fine.
I then went to config.php and followed the link to _configurator and get the above route error. I also get the same error when trying to load _profiler.
Here is my route file:
asf_core_homepage:
pattern: /
defaults: { _controller: ASFCoreBundle:Home:index }
And the Symfony routing_dev file:
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_configurator:
resource: "#SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix: /_configurator
_main:
resource: routing.yml
Any ideas why this is happening?
EDIT
I have narrowed it down and it looks like it has nothing to do with symfony. I have it installed in a subfolder of root so I use htaccess to rewrite all requests from /forum to /forum/web
RewriteRule forum/(.*) /forum/web/$1 [L]
If I point the browser to /forum/app_dev.php it works fine. the error comes from /forum/app_dev.php/_configurator
But if I skip the rewrite rule and use /forum/web/app_dev.php/_configurator it works fine.
http://localhost/Symfony/web/app.php/demo/hello/Alex
shows
"Cannot import resource "#AcmeDemoBundle/Controller/SecuredController.php" from "D:/UserData/WWWRoot/Symfony/app/config/routing.yml". Make sure the "AcmeDemoBundle/Controller/SecuredController.php" bundle is correctly registered and loaded in the application kernel class."
This is my routes of app\config\routing.yml
Internal routing configuration to handle ESI
_internal:
resource: "#FrameworkBundle/Resources/config/routing/internal.xml"
prefix: /_internal
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
_demo_secured:
resource: "#AcmeDemoBundle/Controller/SecuredController.php"
type: annotation
_demo:
resource: "#AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
AcmeDemoBundle is activated in AppKernel for dev and test environments only:
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
}
If you want to access it via app.php — which is for the prod envifonment — you need to move the bundle activation out of the condition.
AFAIK, adding prod in the getEnvironment() options array is OK, assuming you know what bundles dont go to prod.
Have a separate condition to check if its dev or test for other bundles that don't go to prod