ReflectionException: Class SomeClass does not exist – Laravel 4

Note: This post was written while Laravel 4 was still in beta.

When you add a new class (controller) in Laravel 4 you will most probably get

ReflectionException: Class SomeClass does not exist

SomeClass is of course the name of your new class.

Why does this happen? The file/controller is there, the class is there – it works in Laravel 3 so why doesn’t it work in Laravel 4?

The reason for this is that because of the performance reasons Laravel 4 (beta) has a static list of all classes that need to be autoloaded. Since you added your new controller Laravel doesn’t know about it yet. Hopefully this will be automated in the future but right now what you need to do is open your command prompt and type this (on Windows):

 >composer dump-autoload

If you get 'composer' is not recognized as an internal or external command error or Could not open input file: composer then use this version (also go to your project directory where composer.phar is located):

 >php composer.phar dump-autoload

Composer will (re)generate autoload files and your new controller will work.

Warning: file_get_contents(): Unable to find the wrapper “https” – did you forget to enable it when you configured PHP?

I was installing Composer via CLI terminal for a new Laravel project and got this error:

Warning: file_get_contents(): Unable to find the wrapper “https” – did you forget to enable it when you configured PHP? in Command line code on line 1

The solution is easy: Open php.ini file (mine is located in xammp/php/php.ini ) and add (or uncomment) extension=php_openssl.dll in the list of Dynamic Extensions. Restart Apache and it should work.

Laravel Redirect redirects URL to index.php

One of the first things that I do when I start using a new framework (CodeIgniter, Laravel etc) is to remove the default index.php from the URL.

When you install Laravel and you want to remove index.php from the URL simply do this (If you are using Apache, make sure that mod_rewrite is enabled):

Create .htaccess file in your public directory and add this:

<IfModule mod_rewrite.c>
     RewriteEngine on

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d

     RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Many developers do this and forget that there is another step. Everything works fine until you use Laravel’s Redirect class, for example:

return Redirect::to ('someurl'); 

This redirects you to index.php/someurl instead of someurl.

What you need to do is to open application/config/application.php file and you will see this on the line 42:

'index' => 'index.php'

Remove index.php so it becomes

'index' => ''

Save the file and now you have a cleaner, nicer URLs without the index.php.