PHP Symfony Router Error - php

I'm a newbie at Symfony and working on it Symfony documentations. At the lucky number sample(https://symfony.com/doc/current/page_creation.html)
My luckycontroller.php is:
<?php
// src/Controller/LuckyController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class LuckyController
{
public function number()
{
$number = mt_rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
in my routes.yaml file:
app_lucky_number:
path: /lucky/number
controller: App\Controller\LuckyController::number
I've looked for routes on the console:
it shows app_lucky_number route
Then I'm going http://127.0.0.1/projects/todolist/public/lucky/number it gives 404 error.
I didn't understand what did I do wrong. I installed Symfony by using composer.

From https://symfony.com/doc/current/page_creation.html:
Before continuing, make sure you've read the Setup article and can
access your new Symfony app in the browser.
To quickly test Symfony without configuring Apache you can go to your project directory and run this in your terminal:
php bin/console server:start 0.0.0.0:8000
then you should be able to access your new controller using this URI http://localhost:8000/lucky/number
To configure Apache server use this link https://symfony.com/doc/current/setup/web_server_configuration.html

Related

Getting "MongoDB\Driver\Manager Not Found" when using view composers in Laravel

I am using Laravel 7 and Jensseger's MongoDB package.
Everything works fine, except for this:
When I issue php artisan view:clear, I get MongoDB\Driver\Manager Not Found error.
I already have installed the latest version of MongoDB module for php 7.4 and everything else works fine. Meanwhile, I am using a dockerized environment. So, I am totally clueless why this is happening when I try to clear cached views.
This is my ViewServiceProvider file:
class ViewServiceProvider extends ServiceProvider
{
public function register()
{
}
public function boot(HeaderRepository $repository)
{
$header = $repository->getHeader();
View::composer('errors::*', function($view) use($header){
$view->with([
'header' => $header,
]);
});
}
}
My purpose is to create a customized 404 error page, but the header has to be fetched from MongoDB and shared between customized error pages. That's why I'm using view composers. Any other solution is welcome as well.
I suppose, you enter command php artisan view:clear from a local console.
You have choice:
Install mongo drive to your local interpreter
Use artisan from docker container eg docker exec {PHP_CONTAINER} php artisan view:clear

Symfony4 Error loading classes custom folder "Expected to find class... but it was not found"

Problem
I'm trying to setup a custom directory structure
for some shared classes in my Symfony project. I
want to create a custom folder in the root of my
project and I want to use the Symfony auto-load
feature to automatically register services from
that folder.
So I added a custom services namespace to the
services.yaml file:
# src ./config/services.yaml
services:
...
TestNamespace\:
resource: '../TestNamespace/*'
...
And I added an empty class in the custom folder:
# src ./TestNamespace/TestClass.php
namespace TestNamespace;
class TestClass
{
}
When I run the app I get the following error:
(1/2) InvalidArgumentException
Expected to find class "TestNamespace\TestClass" in file
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php"
while importing services from resource
"../TestNamespace/*", but it was not found! Check the
namespace prefix used with the resource.
(2/2) FileLoaderLoadException
Expected to find class "TestNamespace\TestClass" in file
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php" while
importing services from resource "../TestNamespace/*", but it was not
found! Check the namespace prefix used with the resource in
/path/to/ClassLoadErrorDemo/demo/config/services.yaml (which is loaded
in resource "/path/to/ClassLoadErrorDemo/demo/config/services.yaml").
I double checked the paths, namespace and the class
name multiple times and everything seems fine and I
don't understand why I still get the error.
Controllers in the ./src folder seem to load fine.
What am I doing wrong here?
Steps to reproduce
I created a demo repo to isolate the problem.
git clone https://github.com/smoelker/SymfonyClassLoadErrorDemo.git
cd SymfonyClassLoadErrorDemo/demo
composer install
mv TestNamespace/TestClass.php_ TestNamespace/TestClass.php
php bin/console server:start
Update your composer.json autoload setup
{
[...]
"autoload": {
"psr-4": {
"TestNamespace\\": "TestNamespace/",
"": "src/"
}
},
[...]
}
After run: composer dump-autoload and try again.
composer dump-autoload --classmap-authoritative will only work if your src directory is present at the time you run the command.
This can be an issue with multi-stage Docker builds in particular, when you are normally only copying the composer.json/composer.lock into the build image.

Symfony init:acl command missing

I would like to use Symfony's ACL system but I am unable to initialize the database. I followed the steps here How to Use Access Control List (ACL's), but when I run the console command I get There are no commands defined in the "init" namespace.
I have the SecurityBundle defined in my AppKernel new Symfony\Bundle\SecurityBundle\SecurityBundle(). And here is my security.yml
# security.yml
security:
acl:
connection: default
I'm not quite understanding what I'm missing. I do understand though that it's likely a configuration issue. Looking at the Command in symfony's library I see
<?php
class InitAclCommand extends ContainerAwareCommand
{
/**
* {#inheritdoc}
*/
public function isEnabled()
{
if (!$this->getContainer()->has('security.acl.dbal.connection')) {
return false;
}
return parent::isEnabled();
}
//...
}
My guess is isEnabled() is returning false, but I'm not sure what or where I set the configuration for this.
I am using Symfony 3.1.9 and PHP7.0
Thanks
check you have the following package installed:
composer require symfony/security-acl
Try these commands:
composer require symfony/security-acl
Then:
composer update
Then run:
php bin/console init:acl
That should work

How to know which version of Symfony I have?

I know that I have downloaded a Symfony2 project and started with but I have updated my vendor several times and I want to know which version of symfony I have
Any idea ?
Run app/console --version (for Symfony3: bin/console --version), it should give you a pretty good idea. On a random project of mine, the output is:
Symfony version 2.2.0-DEV - app/dev/debug
If you can't access the console, try reading symfony/src/Symfony/Component/HttpKernel/Kernel.php, where the version is hardcoded, for instance:
const VERSION = '2.2.0';
Just in case you are wondering, console creates an instance of Symfony\Bundle\FrameworkBundle\Console\Application. In this class constructor, it uses Symfony\Component\HttpKernel\Kernel::VERSION to initialize its parent constructor.
Although there are already many good answers I'd like to add an option that hasn't been mentioned. Using the command:
php bin/console about
you can get many details about the current project. The first section is about Symfony itself and looks like this:
-------------------- -------------------------------------------
Symfony
-------------------- -------------------------------------------
Version 4.2.3
End of maintenance 07/2019
End of life 01/2020
-------------------- -------------------------------------------
I find the other information besides the version number very useful.
There are also other sections providing details about the (framework) Kernel, PHP, Environment.
Another way is to look at the source for Symfony\Component\HttpKernel\Kernel for where const VERSION is defined. Example on GitHub
Locally this would be located in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php.
Use the following command in your Terminal/Command Prompt:
php bin/console --version
This will give you your Symfony Version.
also you can check the version of symfony and versions of all other installed packages by running
composer show
or
composer show | grep sonata
to get versions of specific packages like sonata etc.
If you want to dynamicallly display your Symfony 2 version in pages, for example in footer, you can do it this way.
Create a service:
<?php
namespace Project\Bundle\DuBundle\Twig;
class SymfonyVersionExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
//this is the name of the function you will use in twig
new \Twig_SimpleFunction('symfony_version', array($this, 'b'))
);
}
public function getName()
{
//return 'number_employees';
return 'symfony_version_extension';
}
public function b()
{
$symfony_version = \Symfony\Component\HttpKernel\Kernel::VERSION;
return $symfony_version;
}
}
Register in service.yml
dut.twig.symfony_version_extension:
class: Project\Bundle\DutBundle\Twig\SymfonyVersionExtension
tags:
- { name: twig.extension }
#arguments: []
And you can call it anywhere. In Controller, wrap it in JSON, or in pages example footer
<p> Built With Symfony {{ symfony_version() }} Version MIT License</p>
Now every time you run composer update to update your vendor, symfony version will also automatically update in your template.I know this is overkill but this is how I do it in my projects and it is working.
we can find the symfony version using Kernel.php file but problem is the Location of Kernal Will changes from version to version (Better Do File Search in you Project Directory)
in symfony 3.0 :
my_project\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php
Check from Controller/ PHP File
$symfony_version = \Symfony\Component\HttpKernel\Kernel::VERSION;
echo $symfony_version; // this will return version; **o/p:3.0.4-DEV**
if you trying with version symfony
please try with
symfony 2 +
cmd>php app/console --version
symfony 3+
cmd>php bin/console --version
for example
D:project>php bin/console --version
Symfony 3.2.8 (kernel: app, env: dev, debug: true)
For Symfony 3.4
Check the constant in this file vendor/symfony/http-kernel/Kernel.php
const VERSION = '3.4.3';
OR
composer show | grep symfony/http-kernel
From inside your Symfony project, you can get the value in PHP this way:
$symfony_version = \Symfony\Component\HttpKernel\Kernel::VERSION;
Maybe the anwswers here are obsolete... in any case, for me running Symfony 4, it is easy
Just type symfony -V from the command line.
This page is the top Google result for search which version symfony using, and the top answers probably don't work anymore.
Apparently I'm on Symfony 5, after running symfony new aqua_note (from SymfonyCasts recommendation).
I had to ultimately run grep -r VERSION . | grep Kernel to see ./vendor/symfony/http-kernel/Kernel.php: public const VERSION = '5.4.2';... at least I think that's correct now.
if you are in app_dev, you can find symfony version at the bottom left corner of the page

Symfony 2 no command for generating doctrine crud

i`m trying to generate CRUD for some entities in Symfony 2, apparently the
generate:doctrine:crud command is unavailable.
[InvalidArgumentException]
Command "generate:doctrine:crud" is not defined.
also , in the list for available commands, I only get one command.
generate
generate:doctrine:entities Generates entity classes and method stubs from your mapping information
is there a bundle or something in the configuration missing, or what is the cause for not having this functionality.
Addition:
The doctrine:generate:crud command is provided by the SensioGeneratorBundle
Please also make sure you have the bundle available and registered in your app/AppKernel.php like this:
class AppKernel extends Kernel
{
public function registerBundles()
{
// ...
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
// ...
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
// ...
}
As in my example the command is also normally only available in the dev environment. Therefore ...
php app/console --env=prod doctrine:generate:crud
.. or any other configuration that uses production enviroment won't work.
doctrine:generate:crud is the command you should use
You can see a list of commands using php app/console list

Categories