为什么自定义指令不能立即反映代码的变化?


Why custom directive not reflecting changes in its code immediately

我在Laravel中编写一个简单的自定义指令。每当我在自定义指令的代码中做了一些改变,它不会反映在视图中,直到我

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

自定义指令代码

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

视图中的指令调用

@molvi('haji') //this will output 'Haji' due to ucwords($2)
//the ucwords() is replaced with strtolower()
@molvi('haji') //this will still output 'Haji'

我正在将单词转换为大写。当我们说我想使用strtolower()而不是ucwords()时,我必须重复上面的步骤来反映更改。

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

因为没有人在StackOverFlow上回答这个问题,我把它贴在了laravel github上。

注意:我只是在github线程上粘贴@lukasgeiter给出的答案。

问题是编译后的视图被缓存,而你不能禁用。但是,您可以清除文件。手动通过删除存储/框架/视图中的所有内容,或者运行命令php artisan view:clear

在Laravel 4和5.0中不支持

这个命令在Laravel 4或5.0中找不到。这是一个在Larvel 5.1中引入的新命令。下面是5.1版的ViewClearCommand代码。

手动添加Laravel 4或5.0的支持

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

注册新命令

在以前的版本中实现的方法是注册新命令。在这方面,Aritsan Development部分很有帮助。

4.2.1

的最终工作代码

我已经在4.2.1上测试了下面的代码。

新增命令文件

app/命令/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!');
}
}

注册新命令

在app/start/artisan.php

中添加以下行
Artisan::resolve('ClearViewCommand');

CLI

现在终于可以运行命令了。每次在自定义指令中更新代码后,你都可以运行这个命令来获取视图中的即时更改。

php artisan view:clear