简体   繁体   English

Laravel base_path() 给出错误 - 调用未定义的方法 baseDir()

[英]Laravel base_path() gives error - Call to undefined method baseDir()

I have a tester class and I'm trying to use the global helper method base_path() , but I get the error below:我有一个测试仪 class,我正在尝试使用全局辅助方法base_path() ,但出现以下错误:

Error: Call to undefined method Illuminate\Container\Container::basePath()
/myproject/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:182
/myproject/tests/Feature/DataCsvSeedTest.php:31
/myproject/vendor/phpunit/phpunit/src/TextUI/Command.php:195
/myproject/vendor/phpunit/phpunit/src/TextUI/Command.php:148

Looks like helper.php in Laravel calls basePath(), and can't find it.看起来Laravel中的helper.php调用了basePath(),但找不到。 Am I missing a namespace or some other setting for the global helper functions?我是否缺少全局辅助函数的名称空间或其他设置? Using Laravel 5.5.使用 Laravel 5.5。

<?php    
namespace Tests\Feature;
use Tests\TestCase;

class DataCsvSeedTest extends TestCase
{
   public function setUp()
   {
    $baseDir = base_path();
    print "basedir = ". $baseDir;
   }
}

Ah... This is a testClass,啊...这是一个testClass,

class DataCsvSeedTest extends TestCase
{
   public function setUp()
   {
     parent::setUp();

     $baseDir = base_path();
     print "basedir = ". $baseDir;
   }
}

when you are overriding setUp() method don't forget to call the parent::Setup() , which will bootstrap the application.当您覆盖setUp()方法时,不要忘记调用parent::Setup() ,这将引导应用程序。

You need to call parent::setUp before using basepath() because your laravel application needs to start first.您需要在使用basepath()之前调用parent::setUp ,因为您的 laravel 应用程序需要先启动。

just add parent::setUp method before using basepath() .只需在使用basepath()之前添加parent::setUp方法。

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

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