Code Monkey home page Code Monkey logo

laravel-url-rewrites's Introduction

Easily add URL rewrites to a Laravel app

Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads

Very easy to use URL rewrite package. Follow the instructions and you're good to go!

You can find an example project on my Github: view example project This example project features the following:

  • Eloquent observers to add URL rewrites automatically
  • Usage of the trait
  • Some use cases

Requirements

This package requires Laravel 8 or higher, PHP 8.1 or higher and a database that supports json fields and functions such as MySQL 5.7 or higher.

Installation

You can install the package via composer:

composer require sparkouttech/laravel-url-rewrites

The package will automatically register itself.

Register the routes the feeds will be displayed on using the rewrites-macro. You need to place it at the bottom of your routes file.

// In routes/web.php
Route::rewrites();

You can publish the migration with:

php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="migrations"

After the migration has been published you can create the url_rewrites table by running the migration:

php artisan migrate

You can optionally publish the config file with:

php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="config"

This is the contents of the published config file:

<?php

return [
    'table-name' => 'url_rewrites',
    'repository' => \RuthgerIdema\UrlRewrite\Repositories\UrlRewriteRepository::class,
    'model' => \RuthgerIdema\UrlRewrite\Entities\UrlRewrite::class,
    'cache' => true,
    'cache-decorator' => \RuthgerIdema\UrlRewrite\Repositories\Decorators\CachingUrlRewriteRepository::class,
    'types' => [
        'product' => [
            'route' => 'product',
            'attributes' => ['id'],
        ],
        'category' => [
            'route' => 'category',
            'attributes' => ['id'],
        ]
    ],
];

Laravel Nova

Using Laravel Nova? You can publish the Nova class to App/Nova with the following command

php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="nova"

In the near future I will publish a Laravel Nova package with features like reindexing the URL rewrites.

Usage

Forward request

Let's say you've got a controller route 'product/{id}' and you have a product 'Apple Airpods' with id=5. When you visit 'apple-airpods' this package will forward the request to the controller but keeps the clean url.

The following code adds this to the database:

UrlRewrite::create('apple-airpods', 'product/5')

Use named routes

You must specify the types in the config.

UrlRewrite::create('apple-airpods', null, 'product', ["id" => 5])

To regenerate the target path you can use

UrlRewrite::regenerateRoute($urlRewrite)
UrlRewrite::regenerateAll()
UrlRewrite::regenerateRoutesFromType($type)

To automatically add the URL attribute to an Eloquent model, you have to add the HasUrlRewrite trait to an Eloquent model. You also need to add the urlRewriteType and optionally add 'url' to the appends array.

use HasUrlRewrite;
public $urlRewriteType = 'category';
protected $appends = ['url'];

Once this is done you can simply call Model::find(1)->url to get the url of the model.

Redirect

301 redirect

UrlRewrite::create('apple-airpods', 'product/5', null, null, 1)

302 redirect

UrlRewrite::create('apple-airpods', 'product/5', null, null, 2)

Other functions

UrlRewrite::all()
UrlRewrite::find($id)
UrlRewrite::delete($id)
UrlRewrite::update($data, $id)
UrlRewrite::getByRequestPath('apple-airpods')
UrlRewrite::getByTargetPath('product/5')
UrlRewrite::getByTypeAndAttributes('product', ["id" => 5])

Testing

  1. Copy .env.example to .env and fill in your database credentials.
  2. Run composer test.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

laravel-url-rewrites's People

Contributors

prasanth-spark avatar

Watchers

 avatar Sivabharathy avatar Krishnaprakash K avatar

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.