黄昏:如何使用单独的sqlite数据库动态设置路由?

时间:2020-08-10 08:59:25

标签: laravel laravel-dusk

我对此一无所知,不胜感激任何想法或帮助。

这是我的设置:

.env文件

APP_NAME=Laravel
APP_ENV=local
APP_KEY=somekey
APP_DEBUG=true
APP_URL=https://blog.test:44300
APP_LANG=en

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=fresh
DB_USERNAME=homestead
DB_PASSWORD=secret

和相应的黄昏文件

.env.dusk.local

APP_NAME=Laravel
APP_ENV=local
APP_KEY=somekey
APP_DEBUG=true
APP_URL=https://blog.test
APP_LANG=en

DB_CONNECTION=testing_browser

test_browser只是一个sqlite数据库(作为文件)。这里没什么特别的。

问题

出于SEO的原因,我必须在web.php文件中注册一些这样的路由:

    Post::all()
        ->each(function (Post $post) {
            Route::get($post->slug, 'BlogController@page')
                ->name($post->slug)
                ->defaults('slug', $post->slug);
    });

在运行良好的浏览器中。然后,当我开始测试我的应用程序时,我开始遇到一些问题。

我得到了错误:

Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such table: posts

因此,我用以下方式包装了提到的路线:

if (Schema::hasTable('posts')) {
  Post::all()
      ->each(function (Post $post) {
          Route::get($post->slug, 'BlogController@page')
              ->name($post->slug)
              ->defaults('slug', $post->slug);
  });
}

好吧,很酷,现在可以再次使用...。但是我现在注意到了....如果我没有将普通数据库(mysql)迁移到一个数据库。测试将再次无法运行。这是错误:

  Illuminate\Database\QueryException  : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'blog.posts' doesn't exist (SQL: select * from `posts` where exists (select * from `post_translations` where `posts`.`id` = `post_translations`.`post_id` and `locale` = de) and `type` = page)

  at /home/vagrant/blog/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

  Exception trace:

  1   Illuminate\Foundation\Application::Illuminate\Foundation\{closure}()
      [internal]:0

  2   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'blog.posts' doesn't exist")
      /home/vagrant/blog/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:66

  Please use the argument -v to see more details.

经过一些调试,我意识到在测试创建过程中,似乎是使用 .env文件来引导应用程序的。

然后实际上,它使用 .env.dusk.local文件创建了一个单独的prozes。

我认为,我不会改变或不应该研究基本黄昏情况的产生,因为这将再次导致其他问题。

那么解决这个问题的最佳想法/最佳实践是什么?

我尝试过的事情

例如,我尝试通过使用不同的APP_ENV变量来解决该问题,但是如上所述,它总是运行.env文件,因此并没有帮助。

如果应用程序在控制台中时无法运行,我还将整个代码块包装在另一个代码块中。那行得通,但是....它使路由无法进行路由缓存,而我从不实际测试这些站点。

也许我在这里只是觉得有点复杂,有人可以用一种巧妙的方法来解决这个问题?高度赞赏。

0 个答案:

没有答案