> ## Documentation Index
> Fetch the complete documentation index at: https://phiki.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Laravel

> Learn how to use Phiki in a Laravel application.

If you have installed Phiki inside of a Laravel application, there are a couple of additional ways you can use it.

## Facade

If you're manually highlighting code with Phiki, you can replace the usage of `Phiki\Phiki` with a facade.

```php theme={null}
use Phiki\Adapters\Laravel\Facades\Phiki;

$html = Phiki::codeToHtml("<?php echo ...", Grammar::Php, Theme::GithubLight)->toString();
```

### Registering custom extensions

If you wish to register a custom extension, you can do so in the `boot` method of a service provider.

```php theme={null}
use Phiki\Adapters\Laravel\Facades\Phiki;
use App\Phiki\MyCustomExtension;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Phiki::extend(new MyCustomExtension);
    }
}
```

### Registering custom grammars and themes

If you wish to register a custom grammar or theme in a Laravel application, you can do so in the `boot` method of a service provider.

```php theme={null}
use Phiki\Adapters\Laravel\Facades\Phiki;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Phiki::grammar('my-grammar', __DIR__ . '/path/to/grammar.json');
        Phiki::theme('my-theme', __DIR__ . '/path/to/theme.json');
    }
}
```

### Caching

Phiki automatically enables caching when used in a Laravel application. It uses your application's default cache store (`CACHE_STORE`) to cache highlighted code blocks.

If you wish to customize the cache store used by Phiki, you can do so in the `boot` method of a service provider.

```php theme={null}
use Phiki\Adapters\Laravel\Facades\Phiki;
use Illuminate\Support\Facades\Cache;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Phiki::cache(Cache::store('redis'));
    }
}
```

#### Cache invalidation

Phiki's cache key is based on the content of the code block, the grammar, the themes chosen, the gutter setting, and any transformers used.

If any of these change, Phiki will automatically generate a new cache key and re-highlight the code block.

If you need to manually clear Phiki's cache, you can do so by calling the `php artisan cache:clear` command, which will clear the entire cache for your application.

```sh theme={null}
php artisan cache:clear
```

## `Str::markdown()`

If you're using the `Str::markdown()` helper method, you can use Phiki's [CommonMark](/commonmark) extension to highlight code blocks inside of your Markdown.

```php theme={null}
use Illuminate\Support\Str;
use Phiki\Adapters\CommonMark\PhikiExtension;
use Phiki\Phiki;
use Phiki\Theme\Theme;

Str::markdown($markdown, extensions: [
    new PhikiExtension(Theme::GithubLight, resolve(Phiki::class)),
]);
```

### Enabling the gutter

As per the documentation on Phiki's [gutter](/highlighting-code), you can enable the gutter by enabling it in the `PhikiExtension` constructor.

```php theme={null}
use Illuminate\Support\Str;
use Phiki\Adapters\CommonMark\PhikiExtension;
use Phiki\Theme\Theme;
use Phiki\Phiki;

Str::markdown($markdown, extensions: [
    new PhikiExtension(Theme::GithubLight, resolve(Phiki::class), withGutter: true),
]);
```

### Using multiple themes

As per the documentation on [Multiple themes](/multiple-themes), you can also pass in an array of themes to the `PhikiExtension` constructor.

```php theme={null}
use Illuminate\Support\Str;
use Phiki\Adapters\CommonMark\PhikiExtension;
use Phiki\Phiki;
use Phiki\Theme\Theme;

Str::markdown($markdown, extensions: [
    new PhikiExtension([
        'light' => Theme::GithubLight,
        'dark' => Theme::GithubDark,
    ], resolve(Phiki::class)),
]);
```

### Using customizations

If you have registered custom extensions, grammars, or theme using the `Phiki` facade as shown above, you can use them inside of your Markdown by resolving the `Phiki\Phiki` singleton from Laravel's service container and passing it to the `PhikiExtension` constructor.

```php theme={null}
use Illuminate\Support\Str;
use Phiki\Adapters\CommonMark\PhikiExtension;
use Phiki\Theme\Theme;
use Phiki\Phiki;

Str::markdown($markdown, extensions: [
    new PhikiExtension(Theme::GithubLight, resolve(Phiki::class)),
]);
```

## Blade component

If you want to highlight code inside of a Blade view, Phiki provides a Blade component that you can use.

```blade theme={null}
<x-phiki::code grammar="php" theme="github-light">
echo "Hello, world!";
</x-phiki::code>
```

This will highlight the code block using the `Grammar::Php` grammar and the `Theme::GithubLight` theme.

### Enabling the gutter

To enable the gutter, add the `gutter` attribute to the component.

```blade theme={null}
<x-phiki::code grammar="php" theme="github-light" gutter>
echo "Hello, world!";
</x-phiki::code>
```

### Changing the starting line number

To change the starting line number, add the `starting-line` attribute to the component.

```blade theme={null}
<x-phiki::code grammar="php" theme="github-light" gutter :starting-line="5">
echo "Hello, world!";
</x-phiki::code>
```

### Passing code as an attribute

Since Laravel trims whitespace from the slot of a Blade component, you can also pass the code to the component using the `code` attribute.

```blade theme={null}
<x-phiki::code grammar="php" theme="github-light" :code="$myCodeVariable" />
```

This is especially useful if you want to highlight code that is stored in a variable or preserve trailing and leading whitespace.
