无法在phpunit上重新声明Laravel路由方法重复

时间:2016-05-09 18:36:05

标签: php laravel phpunit

我在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

我的routes.php:here 我的UserTest.php:here

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

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

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


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

答案 2 :(得分:0)

我遇到了同样的问题,我通过使用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"]);
          }
      }
  }
}