Laravel的-tojs自定义模板指令如何改写为长尾词?

2026-04-18 09:572阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计533个文字,预计阅读时间需要3分钟。

Laravel的-tojs自定义模板指令如何改写为长尾词?

以下是对原文的简化

由Laravel教程栏目为您介绍Laravel自定义模板指令-tojs,希望对需要的朋友有所帮助!Blade允许您自定义命令,您可以使用directive方法注册命令。当Blade编译器遇到该命令时,它就会执行。

下面由Laravel教程栏目给大家介绍laravel自定义模板指令-tojs ,希望对需要的朋友有所帮助!

Blade 允许你自定义命令,你可以使用 directive 方法注册命令。当 Blade 编译器遇到该命令时,它将会带参数调用提供的回调函数。blade模板可以通过directive方法来自定义模板指定,

tojs指令主要用于PHP自定义一些数据转换为js对象方便js调用

Laravel的-tojs自定义模板指令如何改写为长尾词?

1.创建ToJsServiceProvider

<?php namespace App\Providers; use App\Helpers\ToJs\ToJs; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class ToJsServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { $this->app->singleton('tojs', function () { return new ToJs(); }); /* * The block of code inside this directive indicates * the chosen javascript variables. */ Blade::directive('tojs', function () { return '<script> window.Laravel = ' . json_encode(app('tojs')->get()) . '</script>'; }); } }

2. ToJs方法主要是对数组的一些操作

<?php namespace App\Helpers\ToJs; use Illuminate\Support\Arr; class ToJs { protected $data = []; public function put(array $data) { foreach ($data as $key => $value) { $this->data[$key] = value($value); } return $this; } public function get($key = null, $default = null) { if (!$key) return $this->data; return Arr::get($this->data, $key, $default); } public function forget($keys) { Arr::forget($this->data, $keys); return $this; } }

3.声明facade

namespace App\Helpers\ToJs\Facades; use Illuminate\Support\Facades\Facade; class ToJsFacade extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'tojs'; } }

4.在config数组添加serviceProvider

providers 添加
\App\Providers\ToJsServiceProvider::class

aliases 添加
'ToJs' => \App\Helpers\ToJs\Facades\ToJsFacade::class,

5.为了方便调用可以在写一个helper方法

if (!function_exists('to_js')) { /** * Access the javascript helper. */ function to_js($key = null, $default = null) { if (is_null($key)) { return app('tojs'); } if (is_array($key)) { return app('tojs')->put($key); } return app('tojs')->get($key, $default); } }

在PHP代码需要的地方调用 to_js(['username'=>'test']);

blade模板直接通过 @tojs 就可以在页面渲染出
<script> window.Laravel = {"username":"test"}</script>

以上就是关于laravel自定义模板指令-tojs的详细内容,更多请关注自由互联其它相关文章!

本文共计533个文字,预计阅读时间需要3分钟。

Laravel的-tojs自定义模板指令如何改写为长尾词?

以下是对原文的简化

由Laravel教程栏目为您介绍Laravel自定义模板指令-tojs,希望对需要的朋友有所帮助!Blade允许您自定义命令,您可以使用directive方法注册命令。当Blade编译器遇到该命令时,它就会执行。

下面由Laravel教程栏目给大家介绍laravel自定义模板指令-tojs ,希望对需要的朋友有所帮助!

Blade 允许你自定义命令,你可以使用 directive 方法注册命令。当 Blade 编译器遇到该命令时,它将会带参数调用提供的回调函数。blade模板可以通过directive方法来自定义模板指定,

tojs指令主要用于PHP自定义一些数据转换为js对象方便js调用

Laravel的-tojs自定义模板指令如何改写为长尾词?

1.创建ToJsServiceProvider

<?php namespace App\Providers; use App\Helpers\ToJs\ToJs; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class ToJsServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { $this->app->singleton('tojs', function () { return new ToJs(); }); /* * The block of code inside this directive indicates * the chosen javascript variables. */ Blade::directive('tojs', function () { return '<script> window.Laravel = ' . json_encode(app('tojs')->get()) . '</script>'; }); } }

2. ToJs方法主要是对数组的一些操作

<?php namespace App\Helpers\ToJs; use Illuminate\Support\Arr; class ToJs { protected $data = []; public function put(array $data) { foreach ($data as $key => $value) { $this->data[$key] = value($value); } return $this; } public function get($key = null, $default = null) { if (!$key) return $this->data; return Arr::get($this->data, $key, $default); } public function forget($keys) { Arr::forget($this->data, $keys); return $this; } }

3.声明facade

namespace App\Helpers\ToJs\Facades; use Illuminate\Support\Facades\Facade; class ToJsFacade extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'tojs'; } }

4.在config数组添加serviceProvider

providers 添加
\App\Providers\ToJsServiceProvider::class

aliases 添加
'ToJs' => \App\Helpers\ToJs\Facades\ToJsFacade::class,

5.为了方便调用可以在写一个helper方法

if (!function_exists('to_js')) { /** * Access the javascript helper. */ function to_js($key = null, $default = null) { if (is_null($key)) { return app('tojs'); } if (is_array($key)) { return app('tojs')->put($key); } return app('tojs')->get($key, $default); } }

在PHP代码需要的地方调用 to_js(['username'=>'test']);

blade模板直接通过 @tojs 就可以在页面渲染出
<script> window.Laravel = {"username":"test"}</script>

以上就是关于laravel自定义模板指令-tojs的详细内容,更多请关注自由互联其它相关文章!