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

dev123's avatar

How to return to particular view using particular url?

$url = $request->url();

     $url = $url.'/'.$city;

    echo view('carasouel')->with('events',$events);
    return redirect($url);

i want redirect and view at a time from controller , anyone please help me out?

0 likes
8 replies
tykus's avatar

i want redirect and view

No. One response, that's all you're getting. What problem are you trying to solve?

dev123's avatar

I want generate url dynamically so .. i have redirect() helper to redirect but I want view to return for that url

tykus's avatar

If you are redirecting somewhere, then the app should be able to serve that URL, and there you should return the view, e.g. user visits https://www.your-domain.com, your controller appends some $city variable and redirects to https://www.your-domain.com/the-city which is a different route, that returns the required view:

I am showing Route closures but the functionality can be extracted to controller methods (also assumes that city might be dynamic?):

Route::get('/', function () {
    $url = $request->url() . '/' . $city;
    return redirect($url);
});

Route::get('/{city}', function () {
    return view('carasouel')->with('events',$events);
});
dev123's avatar

Route::get('/', function () { $url = $request->url() . '/' . $city; return redirect($url); });

how to mention controller in that code?

tykus's avatar

This is Laravel 101... simply extract the Closure to a method in your controller:

Route::get('/', 'YourController@methodName'});
class YourController
{
    public function methodName ()
    {
        // not clear where $city comes from in your original code... 

        $url = $request->url() . '/' . $city;
        return redirect($url); 
    }
}
dev123's avatar

city will come from from request. then at that time i want to get url as

https://www.your-domain.com/cityname and releated city page should also display.

in form action what should i kept as

form class="city-search" autocomplete="off" action={{ route('eventcity') }} method="get"> {!! csrf_field() !!}

this is route :

Route::get('event',[function(\Illuminate\Http\Request $request){ $url="event/$request->search"; return redirect($url); }])->name('eventcity');

Route::get('event/{city}','HomeController@eventsByCity');

this is controller:

public function eventsByCity(Request $request) {

    $city = $request['search'];

        //here will be query to get particular events based on that city

    return view('carasouel')->with('events',$events);
}

hope now it is clear.. can you help me out

tykus's avatar

After redirecting, you are now receiving the $city from the URI wildcard, not from the Request, so the method should accept that parameter:

public function eventsByCity(Request $request, $city)
{
    //here will be query to get particular events based on that city

    return view('carasouel')->with('events',$events);
}

Please or to participate in this conversation.