【问题标题】:View composer not working properly in testing mode in Laravel 5.2在 Laravel 5.2 的测试模式下查看作曲家无法正常工作
【发布时间】:2016-01-16 18:10:00
【问题描述】:

我有一个这样写的视图作曲家

view()->composer('masterbox.partials.pipeline', function($view) {
  // Some vars and code
});

在我的一个观点中,我会这样做

@include('masterbox.partials.pipeline', ['my_var' => 1])

当我在浏览器上尝试时,一切都很好,但是当我运行一个简单的测试时,一切都崩溃了……经过一些调试,我发现闭包根本没有执行。

$this->visit('/connect/customer/subscribe')
      ->type($faker->firstName, 'first_name')
      ->type($faker->firstName, 'first_name')
      ->type($faker->lastName, 'last_name')
      ->type($faker->email, 'email')
      ->type($faker->phoneNumber, 'phone')
      ->type($password, 'password')
      ->type($password, 'password_confirmation')
      ->press("S'inscrire");

注意:它访问一个页面,填写表单并订阅,然后它重定向到带有@include的页面,它返回一个大错误,部分是

exception 'ErrorException' with message 'Undefined variable: my_var' in /Users/Loschcode/Google Drive/projects/my_project_lo/website/storage/framework/views/7e11f284c02bc38adc60b5f8a0545df65d7cf5ec.php:7

恐怕这是个问题,这是我几天前下载的新 Laravel 5.2。有什么猜测吗?有什么方法可以调试吗?谢谢

【问题讨论】:

    标签: php unit-testing laravel-5 functional-testing


    【解决方案1】:

    工作解决方案

    我最终尝试了任何事情。我的问题是我的服务提供商组织。

        namespace App\Providers;
    
        use Illuminate\Support\ServiceProvider;
    
        class ComposerServiceProvider extends ServiceProvider
        {
            /**
             * Bootstrap the application services.
             *
             * @return void
             */
            public function boot()
            {
    
              foreach (glob(app_path().'/Http/ViewComposers/*.php') as $filename){  
                require_once($filename);
              }
    
            }
    
            /**
             * Register the application services.
             *
             * @return void
             */
            public function register()
            {
                //
            }
        }
    

    如果您有类似的组织和问题,请将require_once 替换为简单的require,一切都会好起来的。

        namespace App\Providers;
    
        use Illuminate\Support\ServiceProvider;
    
        class ComposerServiceProvider extends ServiceProvider
        {
            /**
             * Bootstrap the application services.
             *
             * @return void
             */
            public function boot()
            {
    
              foreach (glob(app_path().'/Http/ViewComposers/*.php') as $filename){  
                require($filename);
              }
    
            }
    
            /**
             * Register the application services.
             *
             * @return void
             */
            public function register()
            {
                //
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-24
      • 2016-12-05
      • 1970-01-01
      • 2016-07-18
      • 2016-04-11
      • 2016-10-30
      • 1970-01-01
      • 2017-03-17
      • 2022-01-20
      相关资源
      最近更新 更多