【问题标题】:Laravel base_path() gives error - Call to undefined method baseDir()Laravel base_path() 给出错误 - 调用未定义的方法 baseDir()
【发布时间】:2018-07-03 01:54:57
【问题描述】:

我有一个测试器类,我正在尝试使用全局帮助器方法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

看起来像 Laravel 中的 helper.php 调用 basePath(),但找不到。我是否缺少全局辅助函数的名称空间或其他设置?使用 Laravel 5.5。

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

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

【问题讨论】:

  • composer dump-autoload
  • composer dump_autoload 不起作用。
  • 请查看答案

标签: php laravel laravel-5.5


【解决方案1】:

啊……这是一个testClass,

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

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

当您覆盖 setUp() 方法时,不要忘记调用 parent::Setup(),它将引导应用程序。

【讨论】:

  • 工作就像一个魅力。谢谢。
  • @Les 请把它标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
相关资源
最近更新 更多