我正在 Laravel 中编写一个简单的自定义指令。每当我对自定义指令的代码进行一些更改时,它不会反射(reflect)在 View 中,直到我
- 注释 View 中的指令。
- 重新加载页面
- 取消指令的注释
- 重新加载页面以最终获得更改
global.php 中的自定义指令代码
Blade::extend(function($value, $compiler)
{
$pattern = $compiler->createMatcher('molvi');
return preg_replace($pattern, '$1<?php echo ucwords($2); ?>', $value);
});
View 中的指令调用
@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()
时,我必须重复上述步骤以反射(reflect)更改。
更新
我已尝试使用 this thread 中描述的各种方法清除缓存但仍然没有成功。
更新
由于在 StackOverFlow 上没有人回答这个问题,我已将其发布在 laravel github 上.
最佳答案
注意:我只是粘贴@lukasgeiter 在 github thread 上给出的答案.
The problem is that the compiled views are cached and you can't disable that. You can however clear the files. Either manually by deleting everything in storage/framework/views or by running the command
php artisan view:clear
在 Laravel 4 或 5.0 中不支持
此命令在 Laravel 4 或 5.0 中找不到。它是一个新命令,在 Larvel 5.1 中引入。这是 ViewClearCommand来自 5.1 的代码。
在 Laravel 4 或 5.0 中手动添加支持
您可以在 Laravel 4 或 5.0 中手动添加支持。
注册新命令
以前版本的实现方式是注册新命令。 Aritsan Development部分在这方面很有帮助。
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!');
}
}
注册新命令
在 app/start/artisan.php 中添加以下行
Artisan::resolve('ClearViewCommand');
命令行
现在终于可以运行命令了。每次更新自定义指令中的代码后,您都可以运行此命令以立即更改 View 。
php artisan view:clear
关于php - 为什么自定义指令不立即反射(reflect)其代码中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586518/