I need to compare these two strings, so for example:
if($this->getRequest()->headers->get('referer') != $this->generateUrl('foo')) {}
The thing is that the referrer here gives me the full url address:
http://website.com/foo
And the generateUrl() method gives me only the following:
/foo
How can I solve this?
You can generate an absolute url using:
$this->generateUrl('route_name', $parameters, true))
$parameters can be null ... see the API reference.
Related
I want to build RESTful API with URLs something like:
First route: http://example.com/api/{element_name}/aaa/{related_name} and
Second route: http://example.com/api/{element_name}/bbb/{related_name}.
Everything is simple and easy when element_name is integer or simple text.
Things get complicated when parameter {element_name} has "/" char in the name, because even if I encode / by %2f (url encode) routing will decode %2f before process routes.
For example when I want to generate URL to first route and I have {element_name} = xyz and {related_name} = ooo then the URL will be http://example.com/api/xyz/aaa/ooo and it's OK.
But when I have {element_name} = xyz/bbb and {related_name} = ooo then the URL should be: http://example.com/api/xyz%2fbbb/aaa/ooo but routing first will decode url and make: http://example.com/api/xyz/bbb/aaa/ooo and it isn't OK because doesn't match to first route.
How I should do that?
All you need to do is to add requirement while configuring the routes in your controller. Like so :
class DefaultController
{
/**
* #Route("/share/{token}", name="share", requirements={"token"=".+"})
*/
public function share($token)
{
// ...
}
}
it's explained in the SF doc: http://symfony.com/doc/current/routing/slash_in_parameter.html
I build the project with retrofit-2.2.0. I wanted to request a full url, but that is failed
public interface FileRetrofitServer {
#Streaming
#GET
Call<ResponseBody> downloadFileAsync(#Url String fileUrl);
}
it return the message as the below:
java.lang.IllegalStateException: Base URL required.
I have got the answer. Base url must not be empty, you can set a default value, and then the full url will replace the Base url.
I'd like to map a servlet for every url ending with "/jsfinspector". For example:
http://localhost/myapp/pages/somewhere/jsfinspector
http://localhost/myapp/jsfinspector
Is it possible to do that? In a very simple way, without declaring all possible url patterns in web.xml?
The Servlet API doesn't support that.
Your best bet is creating a #WebFilter("/*") which forwards to #WebServlet("/jsfinspector") when the URL matches, as shown below:
if (request.getRequestURI().endsWith("/jsfinspector")) {
request.getRequestDispatcher("/jsfinspector").forward(request, response);
} else {
chain.doFilter(request, response);
}
You can if necessary extract the original request URI in servlet as below:
String originalRequestURI = (String) request.getAttribute(RequestDispachter.FORWARD_REQUEST_URI);
You could think about creating a filter to intercept every request and eventually redirect the flow. https://docs.oracle.com/javaee/6/tutorial/doc/bnagb.html
We have files behind authentication, and I want to do different things for post-authentication redirect if the user entered the application using a URL of a file versus a URL of an HTML resource.
I have a URL: https://subdomain.domain.com/resource/45/identifiers/567/here/11abdf51e3d7-some%20file%20name.png/download. I want to get the route name for this URL.
app/console router:debug outputs this: _route_name GET ANY subdomain.domain.{tld} /resource/{id2}/identifiers/{id2}/here/{id3}/download.
Symfony has a Routing component (http://symfony.com/doc/current/book/routing.html), and I'm trying to call match() on an instance of Symfony\Bundle\FrameworkBundle\Routing\Router as provided by Symfony IOC. I have tried with with the domain and without the domain, but they both create a MethodNotAllowed exception because the route cannot be found. How can I match this URL to a route?
Maybe a bit late but as I was facing the same problem, what I come to is something like
$request = Request::create($targetPath, Request::METHOD_GET, [], [], [], $_SERVER);
try {
$matches = $router->matchRequest($request);
} catch (\Exception $e) {
// throw a 400
}
The key part is to use $_SERVER superglobal array in order to have all things setted straight away.
According to this, Symfony uses current request's HTTP method while matching. I guess your controller serves POST request, while your download links are GET.
The route name is available in the _route_name attribute of the Request object: $request->attributes->get('_route_name').
You can do something like this ton get the route name:
public/protected/private function getRefererRoute(Request $request = null)
{
if ($request == null)
$request = $this->getRequest();
//look for the referer route
$referer = $request->headers->get('referer');
$path = substr($referer, strpos($referer, $request->getBaseUrl()));
$path = str_replace($request->getBaseUrl(), '', $lastPath);
$matcher = $this->get('router')->getMatcher();
$parameters = $matcher->match($path);
$route = $parameters['_route'];
return $route;
}
EDIT:
I forgot to explain what I was doing. So basicly you are getting the page url ($referer) then taking out your website's base url with str_replace and then trying to match the remaining part of the path with a know route pattern using route matcher.
EDIT2:
Obviously you need to have this inside you controller if you want to be able to use $this->get(...)
I am successfully using the 'Friendly URL' module in ASP.NET 4.5
In route config I can add a route like this:
routes.MapPageRoute("mypage", "mypage/{mypageName}", "~/mypage.aspx");
for a URL like this:
mysite.com/mypage/hello
in the page 'mypage.aspx' I can get URL segments like this:
using Microsoft.AspNet.FriendlyUrls;
// Get URL segments
IList<string> segments = Request.GetFriendlyUrlSegments();
if (segments.Count > 0)
{
// Get first segment
string url = segments[0];
}
However, I cannot get this working for root URL's. e.g. 'my site.com/ttee'
I want to get 'ttee' and pass it into a page. But 'Request.GetFriendlyUrlSegments()' returns 0 for the root.
How best can I do this?
routes.MapPageRoute("mypage", "mypage/{mypageName}", "~/mypage.aspx");
This will work only for URLs in this format:
www.example.com/mypage/changingparthere
If you want to make it
www.example.com/changablemypage
Set it to:
routes.MapPageRoute("mypage", "{mypageName}", "~/mypage.aspx");
But as you can see, it will catch literally everything. So make sure it is the last routing on Global.asax.