0

I have created new Class Called "song".

Location of the class app/ClassName , so its app/songs.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent {
//put your code here
}

My Route:

Route::get('/','SongsController@index');

My SongsController.php location: app/Http/controller/SongsController.php

Wanting to correspond my database table called "songs".

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Song;

class SongsController extends Controller
{

public function index()
{
$songs = Song::get();

return view('pages.songsList',compact('songs'));
}

Now with all these code, I get a simple error says as below,

FatalErrorException in SongsController.php line 19:
Class 'App\Song' not found

If I use without adding class "Song" model works fine. The code is as below

DB::table('songs)->get();

Thanks in Advance, Sorry I am new to Lavaral 5.

4
  • 2
    Class is called song and file is called songs. They need to match. It should be Song.php Commented Dec 26, 2015 at 18:51
  • Hi @BenSwinburne, Thanks for instant answer. Yes As soon as I match class name and file name the error has vanish. Commented Dec 26, 2015 at 18:55
  • Can you explain why do we need to match class and file name as same. Commented Dec 26, 2015 at 18:55
  • I've explained in an answer below Commented Dec 26, 2015 at 19:05

2 Answers 2

2

Your class is called Song and the file is called songs.php. You need to name the file Song.php in order for it to be found.

Composer generates a PSR-4 compatible autoloader.

You'll notice in your composer.json file you have a section like this

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

This tells the Autoloader that anything in the App namespace should be found within the app directory.

When classes are called, any registered autoloaders attempt to load the class file. This means that App\Song looks for app/Song.php unless you specify otherwise.

From the specification

When loading a file that corresponds to a fully qualified class name ...

A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a "namespace prefix") corresponds to at least one "base directory".

The contiguous sub-namespace names after the "namespace prefix" correspond to a subdirectory within a "base directory", in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this command :

composer dump-autoload -o

EDIT : this command update the autoloader with the new classes. More info : https://getcomposer.org/doc/03-cli.md#dump-autoload

1 Comment

Could you please edit in an explanation of why this code answers the question? Code-only answers are discouraged, because they don't teach the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.