簡體   English   中英

為什么自定義指令不能立即反映其代碼中的更改

[英]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, '$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上

注意:我只是將@lukasgeiter給出的答案粘貼到github thread上

問題是已編譯的視圖被緩存,您不能禁用它。 但是,您可以清除文件。 手動刪除存儲/框架/視圖中的所有內容,或運行命令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開發部分在這方面很有幫助。

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');

命令行界面

現在終於可以運行命令了。 自定義指令中的每次更新代碼后,您都可以運行此命令以立即更改視圖。

php artisan view:clear

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM