为什么自定义指令不能立即反映其代码中的更改

hhs*_*diq 6 php templating laravel blade laravel-4

我在Laravel写了一个简单的自定义指令.每当我对自定义指令的代码进行一些更改时,它就不会反映在视图中直到我

  • 在视图中注释该指令.
  • 重新加载页面
  • 取消注释该指令
  • 重新加载页面以最终获得更改

global.php中的自定义指令代码

Blade::extend(function($value, $compiler)
{
    $pattern = $compiler->createMatcher('molvi');
    return preg_replace($pattern, '$1<?php echo ucwords($2); ?>', $value);
});
Run Code Online (Sandbox Code Playgroud)

指令电话在视野中

@molvi('haji') //this will output 'Haji' due to ucwords($2)

//the ucwords() is replaced with strtolower()
@molvi('haji') //this will still output 'Haji'
Run Code Online (Sandbox Code Playgroud)

我将这些单词转换为大写.当我们想要使用strtolower()而不是代替时ucwords(),我必须重复上述步骤来反映变化.

UPDATE

我试图用这个线程中描述的各种方法清除缓存,但仍然没有成功.

UPDATE

由于没有人在StackOverFlow上回答这个问题,我已将它发布在laravel github上.

hhs*_*diq 3

注意:我只是将 @lukasgeiter 给出的答案粘贴到github thread上。

问题是编译的视图被缓存,你无法禁用它。但是您可以清除这些文件。手动删除存储/框架/视图中的所有内容或运行命令php artisan view:clear

Laravel 4 或 5.0 不支持

Laravel 4 或 5.0 中未找到此命令。这是一个新命令,在 Lavel 5.1 中引入。这是5.1 中的ViewClearCommand代码。

在 Laravel 4 或 5.0 中手动添加支持

您可以在 Laravel 4 或 5.0 中手动添加支持。

注册新命令

以前版本的实现方式是注册新命令。Aritsan发展部分在这方面很有帮助。

4.2.1 的最终工作代码

我在4.2.1上测试了以下代码。

添加新的命令文件

应用程序/命令/ClearViewCommmand.php

<?php

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ClearViewCommand extends Command {
/**
 * The console command name.
 *
 * @var string
 */
protected $name = 'view:clear';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Clear all compiled view files';

protected $files;
/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct(Filesystem $files)
{
    parent::__construct();

    $this->files = $files;
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function fire()
{
    //this path may be different for 5.0
    $views = $this->files->glob(storage_path().'/views/*');
    foreach ($views as $view) {
        $this->files->delete($view);
    }
    $this->info('Compiled views cleared!');
}

}
Run Code Online (Sandbox Code Playgroud)

注册新命令

在 app/start/artisan.php 中添加以下行

Artisan::resolve('ClearViewCommand');
Run Code Online (Sandbox Code Playgroud)

命令行界面

现在终于可以运行命令了。每次更新自定义指令中的代码后,您可以运行此命令以立即更改视图。

php artisan view:clear
Run Code Online (Sandbox Code Playgroud)