taylorpmt / short-code
Laravel support
v0.0.1
2022-04-06 14:02 UTC
Requires
- php: ^7.2|^8.0
- illuminate/contracts: 5.6.x|5.7.x|5.8.x|^6.0|^7.0|^8.0|^9.0
- illuminate/support: 5.6.x|5.7.x|5.8.x|^6.0|^7.0|^8.0|^9.0
- illuminate/view: 5.6.x|5.7.x|5.8.x|^6.0|^7.0|^8.0|^9.0
README
Laravel-Shortcodes
Installation
You can install the package via composer:
composer require taylorpmt/short-code
After updating composer, add the ServiceProvider to the providers array in config/app.php
Required
- illuminate/view 5.6+
- illuminate/support 5.6+
- illuminate/contracts 5.6+
- PHP 7.2+
Usage
Taylorpmt\ShortCode\Providers\ShortcodesServiceProvider::class
You can use the facade for shorter code. Add this to your aliases:
'Shortcode' => Taylorpmt\ShortCode\Facades\Shortcode::class,
OR you can use the AppServiceProvider register for shorter code .
<?php namespace App\Providers; use Taylorpmt\ShortCode\Facades\Shortcode; use Taylorpmt\ShortCode\Providers\ShortcodesServiceProvider; use Illuminate\Foundation\AliasLoader; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { $this->app->register(ShortcodesServiceProvider::class); $loader = AliasLoader::getInstance(); $loader->alias('Shortcode', Shortcode::class); } }
Usage
create File BlockSingleShortCode.php
<?php namespace CMS\Page\Repository\Shortcode; class BlockSingleShortCode { const short_code_name = 'category-item'; /* Shortcode register in providers */ public function register($shortcode, $content, $compiler, $name, $viewData) { // Logic code } }
Register ShortCode in providers
<?php namespace CMS\Page\Providers; use Illuminate\Support\ServiceProvider; use Shortcode; use CMS\Page\Repository\Shortcode\BlockSingleShortCode; class PageProviders extends ServiceProvider { public function register() { Shortcode::register(BlockSingleShortCode::short_code_name, BlockSingleShortCode::class); } }
Shortcode compiling
Defining An Accessor To define an accessor, create a getDataContentAttribute method on your model of the column you wish to access. In this example, we'll define an accessor for the data_content attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of data_content:
public function getDataContentAttribute()
{
return Shortcode::compile($data['data_content']);
}