How to check the url in the controller slim-framework - php

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.

Related

Javascript window.open() but in Laravel

Im developing a website where, in a given moment, i need to redirect the user to an url passing a '_system' parameter. I know this in javaScript is something like window.open('xxx.com', '_system'), but i wanna know if there is way to do this using Laravel and its redirect() response helper and how to do that.
You can just pass the external url into the redirect() method and it will redirect to that url.
For example: return redirect('https://www.google.com');

Redirecting to another page on error with Slim Framework

I want to redirect to a page (error.php, for example) depending on the info in a form in my website. I managed to log an error like this:
if ($date > $curdate) {
return $response
->withStatus(406)
->withHeader('Content-Type', 'text/html')
->write('You can\'t select dates in the future!');
}
How can I do it so it sends you to a page with that error in particular instead of logging/requesting it in the network tab?
Right now I get this:
Request URL:http://raspberrypi/chartAPI/api/{id}/{date1}/{date2}
Request Method:POST
Status Code:406 Not Acceptable
Remote Address:192.168.0.80:80
Referrer Policy:no-referrer-when-downgrade
Which is almost working as intended. What I want to do is for it to send me to "http://raspberrypi/chartAPI/error/406" (for example), and display the contents in a file called 406.php (or error406.php, or error.php or whatever call it).
I managed to do something with this:
return $response->withRedirect("error.php");
But I get this:
Request URL:http://raspberrypi/chartAPI/api/{id}/{date1}/error.php
Request Method:GET
Status Code:405 Method Not Allowed
And slim throws this error:
Method not allowed. Must be one of: POST
Why is it removing {date2}? And why is it asking for a POST method?
This question is a continuation of this one, but due to how this website handles these "old" questions, mine got pushed down and regardless of how many edits I put, it doesn't get any attention anymore.
When you are using withRedirect, use a Route URL not a file to browse
The Error is about using GET Method Where the Route needs POST
I think this is Because you use a file to redirect to,Then the method will be Only GET not POST or others like a browser
I don't Know Why you are using POST whenever you pass parameters in URL ?!

How to access route after certain controller call

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.

Read url data and redirect in route file using Laravel

I will get below mentioned url from 3rd party server..
http://localhost/timespotproject/public/guestacc/chk/defaults/?ids=123e34&ab=00tt&temp=1245678&url=http://test.com%2f&sid=Free+term
When I get this url I have used below mentioned code in routes.php file
Route::get('guestacc/scheck/default/{data}','User#guestcheck');
but it's not redirected.
I have to redirect my action to "User#guestcheck" when i receive above mentioned url...
also if I receive any other url without http://localhost/timespotproject/public/guestacc
(ie http://localhost/timespotproject/public/login)
also i have to redirect User#guestcheck
You have to pass the data chunk like:
http://localhost/timespotproject/public/guestacc/chk/defaults/somedata?ids=123e34&ab=00tt&temp=1245678&url=http://test.com%2f&sid=Free+term
or to get rid of it and remove it form the route if you are just going to use the get params as follows:
Route::get('guestacc/scheck/defaults','User#guestcheck');

Difference between $this->render and $this->redirect Symfony2

What is the difference between $this->render and $this->redirect. Is there a way i can pass arguments with $this->render like in I do with $this->redirect
return $this->render('MedicineUserBundle:User:home.html.twig', array(
'info' => $all,));
Can I do something like this :-
return $this->redirect($this->generateUrl('MedicineUserBundle_login', array(
'info' => $all,)));
Or is there any other way I can pass values with $this->redirect to my template twig file.
And One more question How can i change the url with $this->redirect, eg If i dont have to pass any values to my template file I can do as mentioned above the render will take me to a page like localhost/myproject/home but $->this->redirect will execute the controller but the url wil be the same like localhost/myproject/. Is there anyway i can redirect to another url using redirect
Redirect()
Redirect performs a 301 or 302 redirect to the specified route/location. You can use this to pass in a full URL I believe. Using this method will cause the URL to change in the address bar.
Because Redirect uses a simple 301/302 header to do the redirect there is no way to pass template parameters to the new location except for on the URL as you would do to any controller or URL.
Render()
Render just renders up the template file you tell it to as a response to the current request. With this you can pass in your array of template parameters as normal.
Forward()
There is also Forward which will forward the request to another controller internally sending that controllers response back as the response to the current request without any redirects. Using this method re-routes the request internally without changing the URL in the address bar.
The key difference here between Render and Redirect is that Render is part of the View system and therefore can pass parameters to the tempaltes. Redirect is part of the Controller system and doesn't know anything about the View. You can pass parameters to the route or URL you are redirecting to but the target location must then do something with them to pass them on to the View.

Categories