douyong1885 2018-08-16 13:27
浏览 30
已采纳

spatie / laravel-translatable 5.6无法翻译slu and并抛出404

I have 'courses' table in my database and JSON 'slug' column in it. For example:

| Slug                                                 | 
|------------------------------------------------------|
| {"az": "slug-in-azerbaijani", "ru": "slug-in-russian"|
| {"az": "another-slug-az", "ru": "another-slug-in-rus"|

Now, when I go into course details, I get course details by slug. Here is the controller:

public function detailed($slug) {

        $locale = app()->getLocale();

        $course = Course::where(\DB::raw( "json_extract(slug, '$." . $locale . "')" ), $slug)->firstOrFail();$popularCourses = Course::inRandomOrder()->limit(3)->get();

        if(!$course) {
            return abort(404);
        }

        $data = array(
            'course' => $course,
            'instructor' => $course->instructor
        );

        // Fetch detailed information about instructor and courses that belongs to him
        return view('courses.detailed')->with($data);
    }

However, there is one problem. If I am inside course details and the URL is:

http://localhost:8000/az/courses/slug-in-azerbaijani

then I change the website language to another language, it does not translate the slug. The URL becomes

http://localhost:8000/ru/courses/slug-in-azerbaijani

and it throws 404 page. However, it must be

http://localhost:8000/ru/courses/slug-in-russian

How can I solve this problem? Here is the route.php:

Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
    ],
    function()
    {
        Route::get('/courses', 'CoursesController@index');
        Route::get('/courses/{slug}', 'CoursesController@detailed');
        Route::get('/courses/category/{slug}', 'CoursesController@getCoursesByCategory');
    });
  • 写回答

1条回答 默认 最新

  • duangutian1426 2018-08-19 20:40
    关注

    Actually, I solved the issue. The logic is: When I am inside a detailed course (blog or smth), I get the id of the article. After that when I change the language of the page, I redirect to course/id, not course/slug, and after that I redirect to course/slug. Here is part of my code:

    public function routeById($type, $id) {
    
            if ($type == 'course') {
                $item = Course::find($id);
                $routeName = 'single_course';
    
            } else {
                $item = Category::find($id);
                $routeName = 'courses_by_category';
            }
    
            if (!$item) {
                return abort(404);
            }
    
            return redirect()->route($routeName, $item->slug);
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况