html.twig I am rendering a path like this :
{{ render(path('plugintabs')) }}
In the controller linked to this route if I $request->attributes->get('_route') it will return me 'plugintabs' but I want the global route (the one my browser shows me in the URL bar)
The best would be to have the route and not the route name.
Thanks
$this->container->get('request_stack')->getMasterRequest()->get('_route');
Was the solution for the name
and
$_SERVER["REQUEST_URI"]
for the route
Related
When I develop ejs file, I would like to transit to another ejs by clicking button.
But when I try to click and trnasit to quiz.ejs,nothing happend.
Are there any wrong point in ejs ?
And how to fix them?
Thanks
following is a part of my ejs file.
<button onclick = "href = '/quiz'">start</button>
And following is my Router.
router.get('/quiz',(req,res)=>{
res.render(Views+'quiz.ejs');
});
following is a part of my app.js
app.use('/quiz',Router);
Firstly you don't transit to any ejs file. You transit to another route which renders an ejs file.
So all you need to do is
Create a ejs file, lets say 'quiz.ejs'
Add a route to render this -
router.get('/quiz',(req,res)=>{
res.render('quiz');
});
And then finally redirect the user to quiz route.
<a "href = '/quiz'>To Quiz</button>
Also note that you need <a> anchor tag to navigate not a button.
I have a page Index that has two optional parameters: FolderId and OrganisationId.
If I use #page "{OrganisationId:int?}/{FolderId:int?}" at the start of the razorpages page to make nicer URLs then it becomes impossible to remove a parameter from the URL. The behaviour of the URL helpers changes.
For example the browser is at /Index/1/4 and I want to make a link to /Index/1. The code for the link ought to be
<a asp-area="Files" asp-page="/Index" asp-route-OrganisationId="#Model.Organisation.Id">#Model.Organisation.Name</a>
This works for #page, producing the required /Index/.
However, if the razorpages page begins with #page "{OrganisationId:int?}/{FolderId:int?}" then instead this renders /Index/1/4 which is clearly wrong because it is not what was asked for.
I have tried asp-route-FolderId="" and using asp-route-all-data but the unwanted parameter still appears.
Is it possible to get URL helpers to work correctly with the route templates?
I am using pug as the view engine of a nodejs application. I have a layout that every other current view extends, that contains a navbar with links to common urls across the app.
For example, a link to the signin url would look like :
a(href='/auth/signin')`
This works fine from the root url ('/'), correctly leads to '/auth/signin'.
Within the '/auth' module which contains the routes for '/auth/signin', '/auth/signup' and '/auth/signout', the behavior is different. Instead, the route is concatenated with the current module's name. So for example, within the '/auth/signin' route, the link is actually a link to '/auth/auth/signin'. Clicking on it naturally leads to a 404, but on that page the link to signin is a link to '/auth/auth/auth/signin'.
And so on and so forth.
I don't fully understand what is going on here and how to prevent it. Is there away to link to my routes in absolute terms in pug without straight up typing the full url (which is unpractical for a variety of reasons), the same way you'd use a route helper in Ruby on Rails ?
Solution from the comments:
If you start your href's with a slash then these are interpreted as absolute url's. Then it does not matter in which folder your pug file is located. Please check that your href's start with / always.
I have the user submit a file using POST to /upload, then I want the browser to render index.mustache and tell the browser that the location is /. I attempted to do this with the following code (in multer's onFileUploadComplete):
res.location('/');
res.render('index', {options: 'whatever'});
It renders index, but the browser displays the location as /upload, not / as I desire. What am I doing wrong?
(Answering my own question)
I was able to solve my situation by using res.redirect('/') and placing my {options: 'whatever'} logic elsewhere.
I just started experimenting with node & express, and am trying to load the content of various HTML files into a single web page with Ajax.
I want the URL shown in the address bar to reflect the actual file structure on my server; so for instance when a user clicks a link with href="posts/thePost.html", instead of actually going to that page, I use click(), preventDefault(), and Ajax to load the content in a div, then pushState() and window.onpopstate to make the address bar show the relative path of the file. I do not want to use the hash (#) symbol or queries (?=) or anything like that, I want normal URLs.
This works fine, but when I refresh the page, the file located at the URL (i.e. posts/thePost.html) is displayed instead of index.html with the loaded content. Is there a way to use node to fix this, maybe by intercepting the request and displaying the content of index.html instead? I've tried to search this but haven't been having any luck.
Sorry if this sounds confusing. In short, the behavior that I'm looking for is that no matter what URL the address bar shows when the page is refreshed, index.html should be served up instead.
That's rather easy to do:
function serveIndex(req, res) {
return res.sendfile('index.html');
}
app.get('*', serveIndex);
app.head('*', serveIndex);
You'll likely want to put this after your other routes so you don't end up clobbering them.
var routes = require('./routes/index');
app.use('/',routes);
app.use('/index',routes);
function serveIndex(req, res) {
res.redirect('/');
}
app.get('*', serveIndex);