【问题标题】:Cannot redeclare Laravel routes method duplication on phpunit无法在 phpunit 上重新声明 Laravel 路由方法重复
【发布时间】:2016-05-09 18:36:05
【问题描述】:

我在 Laravel 5.2 的 route.php 文件中有自己的方法。它可以工作,但是当我尝试在 phpunit 上运行测试时,会出现此消息:

Fatal error: Cannot redeclare getRoutes() (previously declared in C:\(...)\ppm\app\Http\routes.php:55) in C:\(...)\ppm\app\Http\routes.php on line 76

我的路线.php:here 我的 UserTest.php:here

【问题讨论】:

  • 我们可以看看你的测试课吗?
  • @JordanPlamondon 是的,我编辑了我的问题

标签: php laravel phpunit


【解决方案1】:

在 Laravel 5.2 中,将 App/Providers/RouteServiceProvide.php 中的 require 更改为 require_once 解决了这个问题。

public function map(Router $router)
{
     $router->group(['namespace' => $this->namespace], function ($router) {
         require_once app_path('Http/routes.php');
     });
}

【讨论】:

  • 但现在我的测试显示:对 [localhost] 的请求失败。收到状态码 [404]。
  • 我认为这是因为没有“/”作为客人的路线。试试把 $this->visit("/") 改成 $this->visit("/login") 吧?
  • 堆栈跟踪显示:“对 [localhost/login] 的请求失败”。您的根路径真的在 localhost 上吗?我认为您错过了将完整路径放在 visit() 函数上。示例:localhost/TestProject/login 将是 $this->visit('/TestProject/login')
  • 同样如此。我通过 php artisan serve 命令运行了我的服务器(我访问了我的网站localhost:8000
  • localhost:8000 在示例测试 ExampleTest::testBasicExample 中失败。 localhost:8000 通过 ExampleTest 并在 UserTest 中仍然失败
【解决方案2】:

我今天遇到了同样的问题。我通过将closure 分配给变量来修复它。像这样的:

$getRoutes = function(){
    Route::get(...);
    // ...
}


Route::group(["middleware" => ["auth"]], $getRoutes);

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,我通过使用 function_exists() 解决了,就像

    if (!function_exists('getRoutes')) {
      function getRoutes($modules)
      {
          foreach ($modules as $module) {
              $actions = $module->getActions();
              $route = $module->getRoute();
    
              Route::get("/" . str_plural($route), ["as" => str_plural($route), "uses" => ucfirst($route) . "Controller@getList"]);
              Route::get("/" . $route . "/find/{search_phrase?}", ["as" => $route . ".search", "uses" => ucfirst($route) . "Controller@getSearchJSON"]);
    
              if ($actions["add"]) {
                  Route::get("/" . $route . "/create", ["as" => $route . ".create", "uses" => ucfirst($route) . "Controller@getAddForm"]);
                  Route::post("/" . $route . "/create", ["as" => $route . ".store", "uses" => ucfirst($route) . "Controller@getAddRequest"]);
              }
              if ($actions["edit"]) {
                  Route::get("/" . $route . "/edit/{id}", ["as" => $route . ".edit", "uses" => ucfirst($route) . "Controller@getEditForm"]);
                  Route::post("/" . $route . "/edit/{id}", ["as" => $route . ".update", "uses" => ucfirst($route) . "Controller@getEditRequest"]);
              }
    
              if ($actions["delete"]) {
                  Route::delete("/" . $route . "/delete/{id}", ["as" => $route . ".delete", "uses" => ucfirst($route) . "Controller@postDelete"]);
              }
          }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-29
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 2012-06-11
      • 2017-07-23
      • 2012-03-05
      相关资源
      最近更新 更多