Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

psychotux's avatar

Laravel redirect url

I have old url and new url. the old url is /topic/titlehere and the new is /topic/categoryhere/titlehere/ So I registered this in my routes :

Route::get('/topic/{title}',function(){
    return redirect()->action('/topic/{category}/{title}','DetailController@index');
});

But I got error Action App\Http\Controllers/topic/{category}/{title} not defined. Anyone can help me about the routes? Thanks for advance

0 likes
10 replies
tykus's avatar

There is a new Route::redirect() available now since 5.5 which just takes the URIs:

Route::redirect('/topic/titlehere', '/topic/categoryhere/titlehere', 307);

You need to ensure that you have a route defined matching the new URI.

Route::get('/topic/{category}/{title}','DetailController@index');

For information, the redirect()->action() method does not take a URI, only the Controller@action

1 like
tykus's avatar

Are you using Laravel >= 5.5?

EDIT available since 5.5

psychotux's avatar

Nope, laravel 5.4 . I don't have access to the server to change laravel version. I can only access the ftp.

tykus's avatar

OK, in that case, back to your original solution. As I mentioned, the redirect()->action() method does not take a URI, only the Controller@action string.

Where are you going to get the {category} segment from?

Route::get('/topic/{title}', function ($title) {
    $category = ''; // how do we determine which category we are redirecting to???

    return redirect()->to("/topic/{$category}/{$title}", 'DetailController@index');
});

Route::get('/topic/{category}/{title}','DetailController@index');
psychotux's avatar

@tykus I wrote {category} cz it's as category from the article. As you know, the old url's already shared so I want to redirect the old url's without share the new url's, and if there is user visits my old url's so it automatically redirect to the new url's. category and title are dynamic

tykus's avatar

Ok, if $title can resolve to a single Article, and that Article belongs to a single Category:

Route::get('/topic/{title}', function ($title) {
        $article = Article::where('title', $title)->firstOrFail();
    $category = $article->category_id;

    return redirect()->to("/topic/{$category}/{$title}", 'DetailController@index');
});

Route::get('/topic/{category}/{title}','DetailController@index');

This will use the category id in the URI, if you want a URL slug, then you would need to use a relationship, and have a slug property on the Category model.

$category = $article->category->slug;
1 like
psychotux's avatar

@tykus is is possible to add query builder on routes ? so I modified the code like this

Route::get('/topic/{title}',function($title){
    $article = DB::table('t_artikel')
            ->join('t_section', 't_section.id_section', '=', 't_artikel.id_section')
            ->where('publish', '=', 'Y')
            ->where('parent_id', '=', 0)
            ->where('date_pub', '<=', date('Y-m-d H:i:s'))
            ->select('t_artikel.*', 't_section.name_section as name_section', 't_artikel.urltitle as urlartikel')
            ->get();
     $category = $article->name_section;
    return redirect()->to ("/topic/".$category,"/".$title);
});

I tried to load and got error Property [name_section] does not exist on this collection instance. but if I changed ->get(); with ->first(); I got error The HTTP status code "0" is not valid.

psychotux's avatar
psychotux
OP
Best Answer
Level 1

Ok solved . I found my way cz @tykus give me the clue. this is the code

Route::get('/topik/{title}',function($title){
    
    
    $article = DB::table('t_artikel')
            ->join('t_section', 't_section.id_section', '=', 't_artikel.id_section')          
            ->where('publish', '=', 'Y')
            ->where('parent_id', '=', 0)
            ->where('tgl_pub', '<=', date('Y-m-d H:i:s'))
            ->where('t_artikel.urltitle', '=', $title)
            ->select('t_artikel.*', 't_section.urltitle as urltitlesec', 't_artikel.urltitle as urlartikel')
            ->first();
     $kategori = $article->urltitlesec;
     return redirect("/topik/{$kategori}/{$title}");
    
});

Thanks @tykus

Please or to participate in this conversation.