简体   繁体   English

无法访问自定义类文件laravel 5中的模型和帮助程序类

[英]Unable to access models and helper classes in custom class file laravel 5

I have created a common class in app/Classes/Common.php but whenever i try to access a model in a class function. 我已经在app / Classes / Common.php中创建了一个通用类,但是每当我尝试在一个类函数中访问一个模型时,就可以这样做。

$new_booking_request = BookingRequest::where('host_id','=',Auth::id())

I am getting this error 我收到此错误

Class 'App\Models\BookingRequest' not found

Even other classes like Auth, URL and Cookie are not working. 甚至Auth,URL和Cookie之类的其他类也不起作用。 Is there a way to bring all classes in my Common class scope? 有没有办法将所有类都带入我的通用类范围?

You get this issue when your namespace is wrong you or you forgot to namespace. 当您的命名空间错误或您忘记了命名空间时,就会出现此问题。

Since common.php is inside App/Classes, inside Common.php do somethng like this: 由于common.php位于App / Classes内部,因此Common.php内部将执行以下操作:

<?php namespace App\Classes;

use View, Auth, URL; 

class Common {
    //class methods
}

Also ensure your model class has the correct namespace, if BookingRequest.php is located inside App\\Models then inside BookingRequest.php do this: 还要确保您的模型类具有正确的名称空间,如果BookingRequest.php位于App \\ Models内,则在BookingRequest.php内执行此操作:

<?php namespace App\Models;

    BookingRequest extends \Eloquent {
        //other definitions
    }

Then if you wish to use BookingRequest.php outside its namespace or in another namespace like so: 然后,如果您希望在其命名空间之外或在另一个命名空间中使用BookingRequest.php,例如:

<?php namespace App\Classes;

use App\Models\BookingRequest;

use View, Auth, URL; 

    class Common {
        //class methods
    }

In Laravel 5 everything is namespaced, make sure your class has a proper namespace and that you are calling it using that same namespace you specified. 在Laravel 5中,所有内容都使用命名空间,请确保您的类具有正确的命名空间,并使用指定的相同命名空间进行调用。

To include classes in another class make sure that you use the use keyword to import the necessary classes on top of your class definition. 要将类包含在另一个类中,请确保使用use关键字在类定义之上导入必要的类。 Also you can call the class globally with the \\ . 您也可以使用\\全局调用该类。 Ex: \\Auth , \\URL and \\Cookie 例如: \\Auth\\URL\\Cookie

For the namespace in L5 here is a quick example: 对于L5中的名称空间,这是一个简单的示例:

<?php namespace App\Models;

class BookingRequest {
  // class definition  
}

then when trying to call that class, either call the full namespace path of the function, or include the function. 然后,当尝试调用该类时,请调用函数的完整名称空间路径,或包含函数。

<?php

class HomeController extends Controller {

  public function index()
  {
    $newBookingRequest = App\Models\BookingRequest::where('host_id','=',Auth::id());
  }

}

OR 要么

<?php namespace App\Controllers;

use App\Models\BookingRequest; // Include the class

class HomeController extends Controller {

  public function index()
  {
    $newBookingRequest = BookingRequest::where('host_id','=',Auth::id());
  }

}

PS: PS:

Please use camelCase when defining class attributes and methods as this helps for a better code-styling and naming conventions when using the L5 framework. 在定义类属性和方法时,请使用camelCase ,因为这有助于在使用L5框架时更好的代码样式和命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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