Last active
April 9, 2019 16:20
-
-
Save nmfzone/c83e4a50e95717057a4af9100bae1db9 to your computer and use it in GitHub Desktop.
Laravel Redirect to Query String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Providers; | |
| use Illuminate\Support\Arr; | |
| use Illuminate\Routing\Redirector; | |
| use Illuminate\Support\ServiceProvider; | |
| class AppServiceProvider extends ServiceProvider | |
| { | |
| /** | |
| * Bootstrap any application services. | |
| * | |
| * @return void | |
| */ | |
| public function boot() | |
| { | |
| /** | |
| * Create a new redirect response to the given queries. | |
| * | |
| * @param array $queries | |
| * @param bool $merge | |
| * @param int $status | |
| * @param array $headers | |
| * @param bool $secure | |
| * @return \Illuminate\Http\RedirectResponse | |
| */ | |
| Redirector::macro('toQuery', function ($queries, $merge = true, $status = 302, $headers = [], $secure = null) { | |
| $request = $this->generator->getRequest(); | |
| $queries = Arr::query($merge ? array_merge($request->query(), $queries) : $queries); | |
| return $this->to($request->url().'?'.$queries, $status, $headers, $secure); | |
| }); | |
| } | |
| /** | |
| * Register any application services. | |
| * | |
| * @return void | |
| */ | |
| public function register() | |
| { | |
| // | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers; | |
| class HomeController extends Controller | |
| { | |
| /** | |
| * Display the listing of the resource. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function index() | |
| { | |
| return redirect()->toQuery(['slug' => 'Post'], false); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment