We help IT Professionals succeed at work.

Why am I getting an error with this simple code in Symfony?

Medium Priority
542 Views
Last Modified: 2017-09-11
I'm going through a tutorial in Symfony and despite the fact that I'm still in the shallow end of the pool, I can't get the very first page in the tutorial to come up and I don't know why.

Let me also mention this: According to the tutorial, I should get a default page when I use this URL: localhost/aqua_note. An error came up when I went to that URL. When I went to this URL: localhost/aqua_note/web, I got the default page they reference in the course.

I removed the DefaultController and replaced it with the code they present which looks like this:

<?php

namespace AppBundle\Controller; //namespace must match the Directory Structure

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

class DefaultController
{
	/**
	*@Route("/genus")
	*/
	public function showAction() //the only rule for a controller function is that it must return a Symfony response object
	{
		return new Response('Under the Sea!');
	}
}


?>

Open in new window


When I go to the page which should show up now as localhost/aqua_note/genus, I get a 500 error.

Why? What am I doing wrong?

Let me also say that I ran into some trouble using the Symfony installer and instead used Composer. If that makes a difference, I'm not sure, but any direction at this point would be very much appreciated.

Thanks!
Comment
Watch Question

RobOwner (Aidellio)
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
First port of call would be to see exactly what 500 error is being reported.  It could be a path issue or a code issue on your side.

Check your php log and post back :)
gr8gonzoConsultant
CERTIFIED EXPERT

Commented:
A 500 error is very generic. It could mean anything. Like Rob said - check your PHP error log for more info (and if you don't have the error log, turn it on in your php.ini file and restart the web server service).

Your code above doesn't really look like there's anything wrong with it from a purely code syntax perspective. Maybe there's a particular class naming convention that needs to be followed? Can you share the tutorial you're following and what specific steps you took when you "removed the DefaultController" ?
Bruce GustPHP Developer

Author

Commented:
Gentlemen!

The tutorial I'm going through is located at https://knpuniversity.com/screencast/symfony/first-page

I'm not at my desk right now, but I'll look at the PHP error log when I get back and see what's going on.

Thanks!
Bruce GustPHP Developer

Author

Commented:
Alright guys, I went back and looked at the PHP log and without going into any great detail, I just made a command decision and decided that my trajectory was so far removed from the tutorial, I was doing better to simply wipe the slate clean and start all over again using the Symfony Installer.

Whereas before I was having troubles, this time I used the Powershell as an Administrator and was able to successfully get the Symphony Installer successfully downloaded and moved to the bin/php directory.

Below you'll see a screenshot of the next task according to the tutorial:

instructions
I was able to make the bat file, but then when I went to run "symfony," I got this:

symfony screenshot
The web address for this process is https://symfony.com/doc/current/setup.html.

What am I doing wrong? When I type "symfony," I get the impression some kind of install is supposed to be happening...?

What do you think?
RobOwner (Aidellio)
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
Try symphony.bat rather than symphony on its own.
Consultant
CERTIFIED EXPERT
Commented:
Unfortunately, I think you were okay before. I think the problem was simply that your class name was "DefaultController" where it should have matched the class file name.

I'm guessing you probably shouldn't be using PowerShell for this - use the command prompt. You can run it as administrator if you are having issues with permissions.
Bruce GustPHP Developer

Author

Commented:
Gentlemen!

I was able to get it resolved.

I went back and tried the installer again to no avail. I went back to Composer, which performed brilliantly and, gonzo, taking my cue from your last statement, rather than feel like something was wrong, I dug a little deeper and found http://symfony.com/doc/current/setup/built_in_web_server.html on the symfony website.

Getting my screen to match the tutorial came down to figuring out how to remove "web" from the URL. What I learned is that symfony, by default, is going to route everything through the "web" directory and removing that comes down to one of two things: Either adjust the htaccess file or use the internal server that comes with Symfony. I went with that and now I'm exactly where I need to be.

Thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.