Skip to content

Latest commit

 

History

History
72 lines (50 loc) · 2.06 KB

slug-field.md

File metadata and controls

72 lines (50 loc) · 2.06 KB

Slug/permalink field

The table to use slug must have column name. It's used to generate slug column.

Add slug/permalink field to your new plugin

  • Open /plugins/<your-plugin>/src/Providers/<YourPlugin>ServiceProvider.php. Add below code to function boot
\SlugHelper::registerModule(YourPluginModel::class);
\SlugHelper::setPrefix(YourPluginModel::class, 'your-prefix');

// "your-prefix" is prefix for your slug field. URL will be http://domain.local/your-prefix/slug-here
  • If you're using form builder to generate forms for your plugin, slug field will be added to your form automatically. If you don't use form builder. To add slug field, use bellow code.

For creating form:

<div class="form-group @if ($errors->has('slug')) has-error @endif">
    {!! Form::permalink('slug', old('slug'), 0, 'your-prefix') !!}
    {!! Form::error('slug', $errors) !!}
</div>

For editing form:

<div class="form-group @if ($errors->has('slug')) has-error @endif">
    {!! Form::permalink('slug', $data->slug, $data->id, \SlugHelper::getPrefix(YourPluginModel::class)) !!}
    {!! Form::error('slug', $errors) !!}
</div>
  • Finally, you need to add a route to handle when users visit http://domain.local/your-prefix/slug-here

Example for route:

Route::group(apply_filters(BASE_FILTER_GROUP_PUBLIC_ROUTE, []), function () {

    Route::get(\SlugHelper::getPrefix(YourPluginModel::class) . '/{slug}, [
        'uses' => 'PublicController@getBySlug',
    ]);

});

Example for controller method:

public function getBySlug($slug, \Botble\Slug\Repositories\Interfaces\SlugInterface $slugRepository)
{
    $slug = $slugRepository->getFirstBy(['key' => $slug, 'reference_type' => YourModel::class]);
    
    if (!$slug) {
        abort(404);
    }
    
    $data = $this->{yourPlugin}Repository->getFirstBy(['id' => $slug->reference_id]);

    if (!$data) {
        abort(404);
    }

    do_action(BASE_ACTION_PUBLIC_RENDER_SINGLE, <PLUGIN>_MODULE_SCREEN_NAME, $data);

    return Theme::scope('your-example-view', compact('data'))->render();
}