Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why custom directive not reflecting changes in its code immediately

I am writing a simple custom directive in Laravel. Whenever I make some changes in the code of custom directive, its not reflected in the view until I

  • Comment the directive in view.
  • Reload the page
  • Uncomment the directive
  • Reload the page to finally get the changes

Custom directive code in global.php

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

Directive call in view

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

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

I am converting the words to uppercase. When lets say I want to use strtolower() instead of ucwords(), I have to repeat above steps to get changes reflected.

UPDATE

I have tried to clear the cache with various methods as described in this thread but still no success.

UPDATE

Since no one is answering this question on StackOverFlow, I have posted it on laravel github.

like image 981
hhsadiq Avatar asked Jul 23 '15 11:07

hhsadiq


Video Answer


1 Answers

Note: I am just pasting the answer given by @lukasgeiter on 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

Not supported in Laravel 4 or 5.0

This command is not found in Laravel 4 or 5.0. Its a new command and introduced in Larvel 5.1. Here is the ViewClearCommand code from 5.1.

Manually add support in Laravel 4 or 5.0

You can manually add support in Laravel 4 or 5.0.

Register new command

The way to achieve it in previous versions is to register new command. The Aritsan Development section is helpful in this regard.

Final working code for 4.2.1

I have tested the following code on 4.2.1.

Add new command file

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

}

Register new command

Add the following line in app/start/artisan.php

Artisan::resolve('ClearViewCommand');

CLI

Now finally you can run the command. After each update of code in custom directive you can run this command to get immediate changes in views.

php artisan view:clear
like image 93
hhsadiq Avatar answered Oct 10 '22 23:10

hhsadiq