Laravel Redirect and Change URL - laravel

I thought this would be an easy task and would be the default functionality ... I was wrong.
I am attempting to redirect the user to the dashboard once logged in, I have tried this several ways:
return redirect('/dashboard');
return redirect()->to('/dashboard');
return redirect()->route('dashboard');
All of these work to display the dashboard, but they do not change the URL, so you cannot tell the user location from the address. Could someone please tell me what is required to achieve this.

i had this problem & saw that my mistake was in the Route file.
i had twice a time [..., 'index'] action defined in the same controller
Route::post('/cart', [CheckoutController::class, 'index'])->name('checkout.cart');
Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout.index');

Related

Set tab name in redirect to route in laravel

in laravel 8 i will redirect the user to a route after doing the query.
There are many tabs on the page where I want the user to be transferred
I wrote the code like this
(Of course I know it's wrong, but I wanted to try, and yet I did not think of another way
return redirect()->route('admin.auth.user.show', $user . "#edit-information")->withFlashSuccess(__('The user was successfully updated.'));
I get an error with this code
What is your solution?
What you can do to activate for example bootstrap tab based on #web paramater is use the redirect() method like this:
return redirect()->route('backend.settings.show', ['eshop' => $eshop->id, '#homeImages']);
Eshop is regular route parameter but '#homeImages' is added to the end of the route which is then picked up by the js on page load.
In Laravel there is a withFragment method, which add a fragment identifier to the URL.
return redirect()->route('admin.auth.user.show', ['user' => $user])->withFragment('edit-information');
https://laravel.com/api/8.x/Illuminate/Http/RedirectResponse.html#method_withFragment

Laravel 5.7 Passing a value to a route in a controller

My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)

How to hide function and parameter from url in codeigniter?

this is my URL.
http://localhost/savyy/home/home_page/Islamabad
i want to show it like
http://localhost/savyy/home
i m using uri routing code
$route['home/(:any)'] = 'home/home_page/$1;
but nothings happen...it shows same URL. can anyone please help me ? please tell me how to hide function name and parameter.
I dont think it will work that way, the request is the route to controller. the request uri will be the same. you will still have the "home/home_page/islamabad" in your url. unless you redirect with the home_page() to home() and save the islamabad in a session.

How to return to intended page in this case

I have the typical scenario of an online newspaper where you have the article and below a form to comment, but where you need to login to comment. So on clicking it takes you to the log page and it should return you to that page once you have authenticated.
So, because the form is part of a page that the user can see without being logged in, I cannot write the middleware for that page created by a PostController, (get route).
The CommentController only has one method, which is the store one for the Form. so, of course, if I placed a middleware for that controller, it would fail because although it would indeed take you to the login page on clicking the submit button, the Intended URL saved would be that Post for the form, so on returning after authenticating, it would take you to a non existent URL page under the name of the Post route for that Form.
I have read about Auth::guards and the like but could not come clear with it,
This
Laravel 5 - After login redirect back to previous page
asks exactly the same question that I do, it is the same scenario, but I dont see how his answer works, because defining a protected variable (and I have that already) like protected $redirectTo in the Auth controller only tells where to go after authenticating, and it is only a fixed route, in my case it takes you to the dashboard. But I dont want to be taken to the dashboard, it has to return to the page where the article and the comment form are.
I found the solution at Laracasts. I must say I really dont understand it, but it works. It adds two functions to the AuthController.
https://laracasts.com/discuss/channels/laravel/redirect-to-page-where-login-button-was-clicked
public function showLoginForm()
{
if(!session()->has('from')){
session()->put('from', url()->previous());
}
return view('auth.login');
}
public function authenticated($request,$user)
{
return redirect(session()->pull('from',$this->redirectTo));
}
It's been a while since i've done this, but this should work normally.
You could add a GET param in your link from the comment section to login page.
http://....com/login?intended=http://yourredirectlink.com/#form
Put the intended url in in the session variable url.intended and after login you redirect like so
Redirect::intended('/');
This will redirect back to the the url '/' if the session variable is not available.

CodeIgniter Pagination is not working with routing

I want to show all users page wise initially. So if I browse http://mydomain/user it will redirect to http://mydomain/user/page/1 and so on. But if I browse http://mydomain/user/1 it will show only single user with id of user.
http://mydomain/user/1 >> it works fine. But as I want pagination so I want to redirect http://mydomain/user to http://mydomain/user/page/1 always
My Routing Info is:
$route['user/(:num)'] = 'user/index/$1';
$route['user'] = 'user/page/$1';
But when I pressed to http://mydomain/user it does not rout to user/page/$1. I have index() method which output to single user information if I give slug. so get a page wise list I used routing and page method. But it is not working. it gives 404 Page not found
Could anybody have solution please..
I think you need to look at the manual for routing:
http://ellislab.com/codeigniter/user-guide/general/routing.html
As far as i can see your second route doesn´t make much sense, you are calling the method page() of your User class - and trying to pass in $1 which does not exist. I think you need to explain better what you want to achieve with your second route - I don´t really see why you need it at all at the moment.

Resources