Adiiia Adiiia - 1 year ago 81
PHP Question

initial project in symfony displaying browser

I'm new to symfony framework so I have wanted to create a simple app in Php Storm. I created a new LuckyController class and inside it a function that returns a random number and I added a route annotation. The problem is that I don't know exactly how to run it according to route. If I click Run-> LuckyController it gives me a blank page. I ve tried to change the url in the browser like :


but it says "this site cannot be accessed" or


where my function is and everything but it gives me "404 Not Found".

My code:

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class LuckyController {
* @Route("/lucky/number")
public function numberAction(){
$number = rand(1,100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'

How to resolve this?

Answer Source

I don't know if your way works, but I always do it the following way. What you tried to do is show a controller as webpage, which is the reason you get an empty view (I can't say for certain if that is why. Never tried your way of creating a HTML page):

 return $this->render('tiwg/luckyNumber.html.twig', array(
          'luckyNumber' => $number
//NOTE:  Make sure the render matches the path where you place the file

So what you do is you tell the controller to figure create a lucky number and then send that value to a twig.html file you render. This can then be called upon using the following

   Your lucky number: {{luckynumber}} (containing the vairable)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download