Code Monkey home page Code Monkey logo

laravel-russian-slugs's Introduction

The package is not supported anymore

Introduction

This package offers easy to use cyrillic slugs like 'Как_вырастить_дерево' and Yandex transliterated 'kak-vyrastis-derevo' slugs and their variations with lowercased letters and different separators.

Installation

Start with editing your Laravel project's composer.json file, add this line to the require section:

"require": {
    ....
    "alexeymezenin/laravel-russian-slugs": "0.9.*"

After that run this command to install package:

composer update

Now, insert these two lines into provider and aliases arrays in config/app.php:

'providers' => [
    ....
    AlexeyMezenin\LaravelRussianSlugs\SlugsServiceProvider::class,
    

'aliases' => [
    ....
    'Slug' => AlexeyMezenin\LaravelRussianSlugs\SlugsFacade::class,

Finally, you need to register config file and slugs-related commands by running:

php artisan vendor:publish

Using slugs

To use package, you need to update your models with thisuse clause:

class Articles extends Model
{
    use \AlexeyMezenin\LaravelRussianSlugs\SlugsTrait;

Then you need to create slug column in a migration:

$table->string('slug');

To use auto slug creation feature add slugFrom property to your model:

protected $slugFrom = 'article_name';

In this case, every time when you're saving data to a DB, package tries to create (but not recreate) a new slug and save it:

$article = Article::create(['article_name' => 'Как вырастить дерево?']);

Of course, that doesn't work with mass inserts and updates when you're updating multiple rows with one query.

Manual slug creation

To create new record with a slug use reslug() method. This will add slug, based on name column:

$article = new Article;
$article->name = 'How to grow a tree?';
$article->reslug('name');
$article->save();

You can update existing record and add a slug:

$article = Article::find(1);
$article->reslug('name');
$article->save();

If slug already exists, but you need to recreate it, use forced reslug:

$article->reslug('name', true);

Alternatively, you can use Slug facade to manually work with slugs:

$article = Article::find(1);
$article->update([
    'slug' => Slug::build($article->name)
    ]);

findBySlug() method allows you to find a model by it's slug:

$slug = 'how-to-grow-a-tree';
$article = Article::findBySlug($slug);
echo $article->name; // Will output "How to grow a tree?"

Configuration

To configure a package you should edit config/seoslugs.php file.

delimiter is a symbol which replaces all spaces in a string. By default it's '_', but also can be '-'.

urlType is a type of slug:

Default is 1. Used for URLs like /категория/книги_в_москве

2 is for traslitterated URLs like /kategoriya/knigi_v_moskve, Yandex rules used to transliterate URL.

keepCapitals is false by default. When true it keeps capital letters in a slug, for example: /книги_в_Москве

slugColumnName sets the name of a slug column. slug by default.

Commands

There are three console commands available in the package:

php artisan slug:auto {table} {column} command creates and executes migration, reslugs a table (creates slugs for all rows) using {column} as source.

php artisan slug:migration {table} command creates migration to add a slug column.

php artisan slug:reslug {table} {column} command creates or recreates slugs for a specified table.

Commands slug:auto and slug:reslug will recreate all slugs, even if they are already exist (forced reslug used).

Copyright

RussianSeoSlugs was written by Alexey Mezenin and released under the MIT License.

laravel-russian-slugs's People

Contributors

alexeymezenin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-russian-slugs's Issues

не работает в последних версиях.

В сервис провайдера не срабатывает регистрация слушателя:

private function registerEvents()
{
    // Create a slug each time when saving to a database, if it doesn't exist yet.
    $this->app['events']->listen('eloquent.saving*', function ($model) {
        if (property_exists($model, 'slugFrom')) {
            $model->reslug();
        }
    });
}

в посл. версиях в функцию попадают имя события и массив данных события(в доках: ($eventName, array $data)
я сделал у себя так:

use Events;

    private function registerEvents()
    {
        // Create a slug each time when saving to a database, if it doesn't exist yet.
        Event::listen('eloquent.saving*', function ($eventName, $model) {
                if (property_exists($model[0], 'slugFrom')) {
                    $model[0]->reslug();
            }
        });
    }

Laravel 9 Fatal Error

PHP Fatal error: Declaration of AlexeyMezenin\LaravelRussianSlugs\SlugMigrationCreator::populateStub($name, $stub, $table) must be compatible with Illuminate\Database\Migrations\MigrationCreator::populateStub($stub, $table) in F:\xampp\htdocs\projects\backend\vendor\alexeymezenin\laravel-russian-slugs\src\SlugMigrationCreator.php on line 33

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.