为什么自定义指令不立即反映其代码中的更改
Why custom directive not reflecting changes in its code immediately
我正在 Laravel 中编写一个简单的自定义指令。每当我对自定义指令的代码进行一些更改时,它不会反映在视图中,直到我
- 注释视图中的指令。
- 重新加载页面
- 取消注释指令
- 重新加载页面以最终获得更改
global.php
中的自定义指令代码
Blade::extend(function($value, $compiler)
{
$pattern = $compiler->createMatcher('molvi');
return preg_replace($pattern, '<?php echo ucwords(); ?>', $value);
});
视图中的指令调用
@molvi('haji') //this will output 'Haji' due to ucwords()
//the ucwords() is replaced with strtolower()
@molvi('haji') //this will still output 'Haji'
我正在将单词转换为大写。当假设我想使用 strtolower()
而不是 ucwords()
时,我必须重复上述步骤以反映更改。
更新
我已经尝试使用this thread中描述的各种方法清除缓存,但仍然没有成功。
更新
由于在 Whosebug 上没有人回答这个问题,我已将其发布在 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 中引入。这是 5.1 中的 ViewClearCommand 代码。
在Laravel 4或5.0
中手动添加支持
您可以在 Laravel 4 或 5.0 中手动添加支持。
注册新命令
以前版本的实现方式是注册新命令。 Aritsan Development 部分在这方面很有帮助。
4.2.1 的最终工作代码
我已经在 4.2.1 上测试了以下代码。
添加新命令文件
app/commands/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
我正在 Laravel 中编写一个简单的自定义指令。每当我对自定义指令的代码进行一些更改时,它不会反映在视图中,直到我
- 注释视图中的指令。
- 重新加载页面
- 取消注释指令
- 重新加载页面以最终获得更改
global.php
中的自定义指令代码Blade::extend(function($value, $compiler)
{
$pattern = $compiler->createMatcher('molvi');
return preg_replace($pattern, '<?php echo ucwords(); ?>', $value);
});
视图中的指令调用
@molvi('haji') //this will output 'Haji' due to ucwords()
//the ucwords() is replaced with strtolower()
@molvi('haji') //this will still output 'Haji'
我正在将单词转换为大写。当假设我想使用 strtolower()
而不是 ucwords()
时,我必须重复上述步骤以反映更改。
更新
我已经尝试使用this thread中描述的各种方法清除缓存,但仍然没有成功。
更新
由于在 Whosebug 上没有人回答这个问题,我已将其发布在 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 中引入。这是 5.1 中的 ViewClearCommand 代码。
在Laravel 4或5.0
中手动添加支持您可以在 Laravel 4 或 5.0 中手动添加支持。
注册新命令
以前版本的实现方式是注册新命令。 Aritsan Development 部分在这方面很有帮助。
4.2.1 的最终工作代码
我已经在 4.2.1 上测试了以下代码。
添加新命令文件
app/commands/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