简体   繁体   中英

Laravel always redirect certain routes

I'm currently writing a tool to manage certain articles, technical documentations, ...

Every article has a part number, which is always in the format xxxx-xxx-xx , where x is a digit. Currently, my routing is set up as follows:

admin/{partnumber}       (List all documentation entries)
admin/{partnumber}/new   (New documentation entry)
admin/{partnumber}/edit  (Edit the article itself)
admin/{partnumber}/{id}  (Edit the documentation with the given id for the article)

Where partnumber must match the following regular expression:

(\d{4}-\d{3}-\d{2}|\d{9})

This is, so I don't necessarily have to type in the dashes. However, to make my urls look a little easier and make them more comprehensible, I'd like to automatically redirect to the entered url, but add the dashes.

example.com/admin/123456789      => example.com/admin/1234-567-89
example.com/admin/123456789/new  => example.com/admin/1234-567-89/new
...

I figured I could create a route for \\d{9} separately, like the following:

Route::get('admin/{partnumber}', 
           'ArticleAdminController@redirectWithDashes')
       ->where('partnumber', '\d{9}');

And then perform a redirect in that controller's function. However, this would only work for admin/{partnumber} , not for any of the other routes.

I can't think of any way to do it, except making every function for all the routes twice, once just for redirection. That seems a little verbose to me though and can't be the solution.

Another idea I had was creating a middleware for the specified routes and redirect to the routes themselves, but with different parameters. But that doesn't seem like a good use case for middleware in my opinion.

Am I missing something? Is this possible at all?

What I would do when absolutely wanting to fix this in laravel is create a middleware, for instance PartnumberMiddleware :

<?php

namespace App\Http\Middleware;

use Closure;

class PartnumberMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (preg_match('/^(?<number>[\d]{18})$/', $request->input('partnumber'), $matches) {
            $partnumberIn = preg_split('/^\d{4}\d{3}\d{2}\d{9}$/', $matches['number']);
            $partnumberMutated = implode('-', $partnumberIn);
            return redirect()->route($request->route()->getActionName(), array_merge($request->route()->parameters(), ['partnumber' => $partnumberMutated]));
        }

        return $next($request);
    }

}

Not tested and you need to fill in the blanks. But this should work once you registered the middleware.

Added the mutation to the partnumber, added getting the route from the request object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM