duanfazhun0383 2015-06-17 17:55
浏览 30

Laravel总是重定向某些路线

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?

  • 写回答

1条回答 默认 最新

  • duanouyong4228 2015-06-18 14:30
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了