I have two routes
Route::post('checkout','CartController#billingSave');
Route::get('success','CartController#billingSave');
So after completing the billingSave method it will redirect to success page..
But if put /success also in url it shows the success page also..though he didn't complete the payment..is there anyway i could let access only the success url after completing the billingSave functionality?
means he can't access success url instead of completing the billingSave method ..or like don't give him access the success url?
You could block the success route using laravel's Middleware https://laravel.com/docs/5.4/middleware You can write your own middleware to check whether billingSave was done.
Change the following route:
Route::get('success','CartController#billingSave');
to
Route::post('success','CartController#billingSave');
so that user cannot access it directly by using the success route in browser, don't forget to show error page here. It can only be accessed by form post method. After data saved successfully submit the form to success route.
Related
I am using Laravel 8.0 to create a web application. I have multiple pages that can only be seen if you are signed into an account. I have it setup so that once you go to the page, it uses a controller function to return the view, but before that it checks if you are logged in with a simple if statement. If you are logged in, it returns the view, else it redirects to login page. I am wanting to make it so after they login it redirects them back to the page they were trying to go before. I used return redirect()->back(); to do this, but it returns them too the login page as it thinks that is where it was before. I use a separate route and controller function to login them in via the form. If anyone could help, I would much appreciate it!
You have to use following code. In your login function just add this
Session::put('previousUrl',url()->previous());
$previous_url = Session::get('previousUrl');
And after login authentication successful just call this
return Session::get('previousUrl');
i prefer use this
redirect()->route('your route name')
because in logic, after submit form, it goes to another route which is /login with POST Request
So when u use this
redirect()->back()
it goes to /login with GET Request which is your login form
Is there a possibility to redirect to an url with GET params using Redirect::to() in Laravel 4.2?
I'm trying to redirect users after login to requested page when they weren't logged in.
If you are not logged in and you visit authorized page, I save the full requested url in session (this url can be something like https://page.com/settings or https://page.com/post/23 or https://page.com/post/23?show=full. Lots of variants).
When you then log in and you have an url stored in your session, I redirect you with Redirect::to(https://page.com/post/23)
In first two cases there is no problem, but when I use Redirect::to(https://page.com/post/23?show=full) GET params are ignored.
Is there an option to redirect to url with GET params included?
There's problem with your application. If I understand you correctly, your query parameters (such as: ?param=true¶m2=1) are not present in your url after redirect.
Consider the following example:
Route::get('/test', function () {
return Redirect::to('http://localhost:8000/test2?with=params');
});
Route::get('/test2', function () {
echo '1';
});
When I try to access: localhost:8000/test I get redirected to: http://localhost:8000/test2?with=params
Which is the expected behavior. Can you post an example where you do that redirect?
Also I'd suggest using: Redirect::intended() which does exactly what you want by itself.
Try this:
You can pass it like https://page.com/post/23/showFull
And you can got it via following way:
Request::segment(3);
Why not Redirect::back()? The auth filter should be able to redirect all the non-authed requests to the login page, then Redirect::back() the user to the previous visited page.
Edit: filter, not middleware.
In Laravel, By default this feature is available if you are using the auth middleware, as given below:
Route::group(array('before' => 'auth'), function(){
//authorized routes
});
Example
If the user without loging in, is accessing the url "example.com/User?abc=123", which is an authorized route.
Then the user will be redirected automatically to the login page.
After successful login the user will be automatically redirected to the url first requested ("example.com/User?abc=123")
I have problem with slim
I have the controllar, and 2 routs send to this controller.
The page html with teig.
The api and show that in json.
Now I need to check, if I request with this url:
/product/{slug}
return the twig (I do the return).
and if I request to url:api/product/{slug}
return me the json (i do the return).
now I don't know how to the the if request.
and how I just to know check what url request that controller.
$request->getUri()->getPath() will give you the current URL.
$request->getAttribute('route') will give you the current Route object.
In Codeigniter I used to call the view function after posting data. Like below;
Ex: I have a show_products() function which will display list of products. When a user add a new product I'm posting data into add_product() function. If the process is successful I will not redirect to the products page, instead load the display function inside the add_product() like this:
//Inside the add_product() function
if(success){
$this->show_products();
}
I think, there is no point of reloading the page again. Since we are already in the post function we can straight away set the view after the database insert.
However in laravel I see people redirecting after posting data.
ex:
//Inside the postProduct() function
if(success){
return Redirect::to('products');
}
I tried;
//Inside the postProduct() function
if(success){
$this->getIndex();// this is my product display function
}
but it didn't work.
Do we have a benefit by loading the view in the post function without redirecting every time?
If so how can I achieve the same thing with the laravel?
Thanks a lot!
It's not about the Laravel, instead, it's about a good or right way of doing things. In other words, it's a good programming practice to redirect to another route/page after you successfully submit a form.
Even in CodeIgniter or plain Php I do like this approach and encourage other developers to do that. So, the question is why this redirect is better than directly calling another method from the same request to show another view/page ?
This is the life cycle of the process:
You post a form to a route/action page.
You validate the submitted data and upon successful validation you insert the submitted data in to your database, otherwise you redirect back to that form with errors and old user inputs.
So. assume that, you have submitted a form and done saving the data into database successfully. After you save it you done something like this:
return View::make('...')->with('success', 'Data saved!');
In this case, your user can see the success message on the screen but what if the user, presses the f5 key from the keyboard to refresh the page (probably, accidentally), the form will be submitted to the same action again and the whole process will be repeated again.
So, if you had a redirect after form submission then, refreshing the page won't make any request to that form again.
Google search result on form resubmit on refresh., check the links, may be first one/two, you'll get better idea about the problem and the benefits of redirection after form submission.
in Codeigniter to redirect page we have redirect() function.
if(success){
redirect('products');
}
You don't have to return Redirect. The reason people use it quite often in larvel is because it's comfy.
You can return something else, eg. a view:
return View::make('home.index')->with('var',$var);
In Laravel, to redirect after doing a POST, you could return a redirect with a named route:
return redirect()->route('my-route-name');
Or if you are within the controller that has the route method you want (eg. the index method, you could do this as well:
return self::index();
When Ive created a new controller, ie in this case Authenticate, Ive also created the folder and file application/views/scripts/authentication/index.phtml
Not a problem when hitting the url http://dev.local/authentication/ but when calling any action ie http://dev.local/authentication/login, I get the error below.
Message: script 'authentication/login.phtml' not found in path (C:\Sites\application\views\scripts\)
Regardless of any changes Im going to make to the login action it shouldnt automatically ask for a new page right? or am I wrong?
By default, each action requires its corresponding view (phtml page). If you want to disable a view/layout for a given action, you can use the following code :
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
EDIT in response to the comment:
I usually don't need to do this, because the actions I have that don't need a view script are redirected/forwared to other actions. For example, once your user is authenticated (i.e. when /authentication/login succeeds), you can redirect him to the home page (or whatever page he was trying to access. Similarly, if the login failed, I simply set an error message in the view and forward to the action that shows the login form.
The only actions for which I use the above code is for actions that are typically called using AJAX and that output some JSON code, for example.