简体   繁体   中英

Why isn't the “user” class found?

I'm building a PHP web application with Laravel and am trying to pass a name and email from a form to my database, but it displays this error:

Fatal error: Uncaught Error: Class 'user' not found in C:\\xampp\\htdocs\\MVC\\app\\controller\\home.php:20 Stack trace: #0 C:\\xampp\\htdocs\\MVC\\app\\core\\app.php(43): home->create('hala', 'hala@yahoo') #1 C:\\xampp\\htdocs\\MVC\\public\\index.php(4): app->__construct() #2 {main} thrown in C:\\xampp\\htdocs\\MVC\\app\\controller\\home.php on line 20

This is the code I'm using for the home page:

class home extends controller
{    
    public function index($name = '')
    {
        $this->view('home/index', ['name'=>$user->name]);
    }

    public function create($username = '', $email = '')
    {
        user::create([
            'username' => $username,
            'email'=> $email
        ]);
    }
}

and the model:

use Illuminate\Database\Eloquent\Model as Eloquent;
class user extends Eloquent 
{
    public $name;
    protected $fillable = ['username','email'];
}

What am I doing wrong and how can I fix it?

In your controller code, you need to include the user class:

require_once("user.class.php"); /* or whatever the file is named */

If this is done automatically and the class is in a different namespace, you need to declare your intent to use it in the controller:

use \my\namespace\user;

Or just use the fully qualified class name in your code:

\my\namespace\user::create();

If you use illuminate/database then chances are you are using composer. Why not add a PSR-4 auto load rule and structure your code accordingly. Eg. composer.json might look like this:

{
    "name": "acme/acme",
    "description": "Acme is this and that",
    "type": "project",
    "require": {
        "php": "^7.2",
        "illuminate/database": "^5.7"
    },
    "autoload": {
      "psr-4": {
        "Acme\\": "src"
      }
    },
    "license": "proprietary"
}

Runing composer install makes you an vendor/autoloader.php and it is the only file you need to require. You put your own code un the Acme (or whatever you chose) namespace. Eg. You put your user model under src/Model/User.php and add your namespace:

<?php

namespace Acme\Model;

use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Eloquent 
{
    public $name;
    protected $fillable = ['username','email'];
}

Your main file might look like this..

<?php 
// entry point file

require_once('vendor/autoload.php');

use Acme\Model\User;
$user = new User();
// ...

Obviously you would make most logic in some class so this should be quite short.

This might seem obvious to people working on recent projects, but I have seen too many projects that still have a static file including all the classes like we did in the olden days. Move your projects to the 2010s now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM