PHP Symfony Semantical Error - php

I created src/Controller file named as sampleController.php:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class sampleController extends Controller
{
/**
* #Route("/hello")
*/
public function number()
{
$number = mt_rand(0, 100);
return $this->render('sample/number.html.twig', array(
'number' => $number,
));
}
}
My twig file is this:
<h1>Number is: {{ number }}</h1>
And routes.yaml is this:
sample_asd:
path: /hello
controller: App\Controller\sampleController::number
I've installed annotations and --dev profiler. But when I navigate to http://localhost:8000/hello it gives:
HTTP 500 Internal Server Error
[Semantical Error] The annotation "#Route" in method App\Controller\sampleController::number() was never imported. Did you maybe forget to add a "use" statement for this annotation? in C:\xampp\htdocs\Projects\symfony_learning\config/routes../../src/Controller/ (which is being imported from "C:\xampp\htdocs\Projects\symfony_learning\config/routes/annotations.yaml"). Make sure annotations are installed and enabled.
I didn't understand what is the problem.
Thanks

You did forget to include annotation class. Try to add this.
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Update
For symfony 4 you should use this path.
use Symfony\Component\Routing\Annotation\Route;

Related

Symfony 4 and PhpSpreadsheet

Currently I try to use this PHP Spreadsheet Bundle for Symfony 4:
https://github.com/roromix/SpreadsheetBundle
But my attempts to use this bundle are not successful.
Can't find it as a service (php bin/console debug:autowiring)
and the way below is also wrong.
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Roromix\Bundle\SpreadsheetBundle\RoromixSpreadsheetBundle;
class TranslationController extends Controller
{
/**
* #Route("/")
*/
public function index()
{
$RoromixSpreadsheetBundle = new RoromixSpreadsheetBundle();
//...
}
}
?>
I think for get service in your controller :
dump($this->get('phpspreadsheet'));
Because service name as phpspreadsheet : https://github.com/roromix/SpreadsheetBundle/blob/master/src/Resources/config/services.yml

Routing annotation with Symfony4 doesn't match cause of DNS?

I'm trying to make the first page creating tutorial
https://symfony.com/doc/current/page_creation.html
So i use the annotation in my src/Controller/LuckyController.php
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function number()
{
$number = mt_rand(0, 100);
return $this->render('lucky.html.twig', array(
'number' => $number,
));
}
}
And when i try to go in this url
(i.e)OVHServer.fr/project/public/lucky/number
I got a 404 Not Found Error
I guess that it's because I didn't put my DNS in the the project/public directory but I want to know if you know a way for let the Url like that and get the road in my controller.
Thank's
DNS is not relevant. The routing takes effect after the configuration of the server.
The public directory of symfony 2 and 3 is named web, it's named public since symfony4
For example :
OVHServer.fr/app.php/lucky/number
OVHServer.fr/web/app.php/lucky/number
OVHServer.fr/project/public/app.php/lucky/number
When your server is configured to hide the app.php it would give you
OVHServer.fr/lucky/number
OVHServer.fr/web/lucky/number
OVHServer.fr/project/public/lucky/number

No route found from another bundle

I have added a second bundle and when I try to open some url from that bundle, I keep getting an error that the route was not found. Adding the same route to the main bundle works perfectly.
What's wrong?
This is my project structure. I also:
- added the UserBundle to AppKernel.php (IDE shows the class exists)
- use AppBundle\Controller namespace in AppBundle, and UserBundle\Controller namespace in UserBundle
The controller I try to access from the UserBundle looks like this:
namespace UserBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
/**
* #Route("/login", name="user_login")
*/
public function loginAction(Request $request)
{
return array();
}
}
I suspect you need to add the UserBundle to your routing.yml configuration. You should have something like this in the routing.yml:
user_bundle:
resource: "#UserBundle/Controller/"
type: annotation

My first controller in symfony

// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function numberAction()
{
$number = rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
I'm new of Symfony framework. I tried this simple code without any result.
That's the server response:
No route found for "GET /lucky/number" 404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
I don't now why the Default controller use the annotation and I can see the homepage of my Symfony application.
I think you need to import your annotation routes. Symfony will scan locations you mention in app/config/routing.yml.
# app/config/routing.yml
lucky:
resource: "#AppBundle/Controller/LuckyController.php"
type: annotation
See Symfony docs for more details
Also try clearing the cache, just to be sure.
You're missing an important part of the route and its name:
/**
* #Route("/lucky/number", name="lucky_number")
*/
More information: http://symfony.com/doc/current/book/routing.html

Class Not Exist when loading entity Symfony 2

I create a bundle and Entity inside the bundle. But when i try to get the entity or repository in controller using the entity manager it give an error that class not exist.
I try to debug using php app/console doctrine:mapping:info
it prompts every thing is correct. Output
Found 4 mapped entities:
[OK] Bitcoin\MyBundle\Entity\ProductCategory
[OK] Bitcoin\MyBundle\Entity\AdminUser
[OK] Bitcoin\MyBundle\Entity\Product
[OK] Bitcoin\MyBundle\Entity\User
My Controller code is as follows
<?php
namespace Bitcoin\MyBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Bitcoin\AdminBundle\Form\Login;
use Bitcoin\AdminBundle\Form\LoginValidate;
class LoginController extends Controller {
public function loginAction(Request $request) {
$user = $this->getDoctrine()->getRepository('User');
echo '<pre>';
prin_r(get_class_methods(get_class($user)));
die;
$pageData = array(
'name' => 'Login',
);
return $this->render('BitcoinAdminBundle:Login:login.html.twig', $pageData);
}
}
When i access this in browser the output is :-
i am previously worked with zend framework but new in symfony2. Any help will appreciate.
i find the issue and it is really a silly mistake.
For get an repository we should have to use the Bundle name with class name like this
$user = $this->getDoctrine()->getRepository('BitcoinMyBundle:User');

Categories