laravel 在哪里放置 Blade::extend
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18329541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to place Blade::extend
提问by Vincent
I want to add the following code to my laravel project to support the break and continue statements in blade.
我想将以下代码添加到我的 Laravel 项目中,以支持 Blade 中的 break 和 continue 语句。
This is the code:
这是代码:
Blade::extend(function($value)
{
return preg_replace('/(\s*)@(break|continue)(\s*)/', '<?php ; ?>', $value);
});
I have no idea however where to place it, any help would be appreciated?
我不知道把它放在哪里,任何帮助将不胜感激?
采纳答案by ciruvan
There's no requirement telling you where you should put the code, you could even put it in your routes.php
(which is a bit messy of course). You only have to make sure that it's loaded when laravel processes a page view.
没有要求告诉你应该把代码放在哪里,你甚至可以把它放在你的routes.php
(当然这有点乱)。你只需要确保它在 laravel 处理页面视图时被加载。
In this case, creating a new file blade_extensions.php
somewhere and including it in start/global.php
might be a good solution.
在这种情况下,在blade_extensions.php
某处创建一个新文件并将其包含在其中start/global.php
可能是一个很好的解决方案。
PS: Be sure to clear out your compiled views, as Blade only recompiles the views if it detects a change, so if you've just plonked in this code it won't work until you clear out the views.
PS:一定要清除你编译的视图,因为 Blade 只会在检测到变化时重新编译视图,所以如果你刚刚输入了这段代码,它在你清除视图之前不会工作。
回答by slp
Laravel 5 alternative
Laravel 5 替代品
1) create app/Providers/BladeServiceProvider.php
1)创建 app/Providers/BladeServiceProvider.php
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class BladeServiceProvider extends ServiceProvider
{
public function boot()
{
/* @datetime($var) */
\Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createOpenMatcher('datetime');
return preg_replace($pattern, '<?php echo ->format(\'m/d/Y H:i\')); ?>', $view);
});
/* @eval($var++) */
\Blade::extend(function($view)
{
return preg_replace('/\@eval\((.+)\)/', '<?php ; ?>', $view);
});
}
public function register()
{
//
}
}
2) in config/app.php
add
2)config/app.php
添加
<?php
return [
// ...
'providers' => [
// ...
'App\Providers\BladeServiceProvider',
3) run php artisan clear-compiled
3)运行 php artisan clear-compiled
4) in your template use @datetime($updated_at)
or @eval($var = 1)
, @eval($var++)
for example
4) 在您的模板中使用@datetime($updated_at)
或@eval($var = 1)
,@eval($var++)
例如
5) important remark
5) 重要备注
blade
templates are cached, try to make a dummy change in blade, this way laravel will recompile the template – sbedulin Feb 9 at 17:43
blade
模板被缓存,尝试在刀片中进行虚拟更改,这样 Laravel 将重新编译模板 – sbedulin 2 月 9 日 17:43
In addition to sbedulin's great solution for Laravel 5
除了 sbedulin 为 Laravel 5 提供的绝佳解决方案
a) run php artisan clear-compiled
might be helpful
a) 跑步php artisan clear-compiled
可能会有所帮助
b) I changed the code for
b) 我更改了代码
$pattern = $compiler->createOpenMatcher('datetime');
$pattern = $compiler->createOpenMatcher('datetime');
and
和
return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\')); ?>', $view);
return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\')); ?>', $view);
because the example from the Laravel 5 Documentationwill not work.
因为Laravel 5 文档中的示例将不起作用。
The exampleis corrected now.
该示例现已更正。
The example was removed.
该示例已删除。
回答by sbedulin
Laravel 5 update:
Laravel 5 更新:
1) You may want to create Extensions\BladeExtensions.php
folder\file on the same level as Models, Providers, Services
folders
1)您可能希望Extensions\BladeExtensions.php
在与Models, Providers, Services
文件夹相同的级别上创建文件夹\文件
2) BladeExtensions.php
2) BladeExtensions.php
<?php namespace App\Extensions;
class BladeExtensions {
public static function register()
{
\Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createMatcher('datetime');
return preg_replace($pattern, '<?php echo ->format(\'m/d/Y H:i\'); ?>', $view);
});
}
}
3) AppServiceProvider.php
3) AppServiceProvider.php
// ...
use App\Extensions\BladeExtensions;
class AppServiceProvider extends ServiceProvider
{
// ...
public function register()
{
// ...
BladeExtensions::register();
}
}