I'm working in a vue file inside of a Symfony project and I want to find the absolute path from a route. Usually I do something like this:
this.$http({
url: 'products/edit/'+product.id,
method: 'PUT',
params : params
[...]
and it works great. But now I don't need to use this.$http because I'm trying to do this:
<div>
{{ getUrl(product) }}
</div>
and I have my function:
getUrl(product) : {
let url = '/products/edit'+product.id
return url
}
but obviously it only displays /products/edit/xxx
instead of mysite.com/products/edit/xxx in production or localhost/products/edit/xxx in local.
In my routing file I have:
update_product:
path: /products/edit/{product}
defaults: { _controller: RBProductsBundle:Product:edit }
Try this:
methods:
getFullUrl (product) {
return location.origin + this.getUrl(product)
}
}
...
{{ getFullUrl(product) }}
...
Location object has info about the origin & path to the currently opened page.
Read more about "location" here
Related
In my Polymer app, I want to read a JSON file, for this I use an element. Part of the url is send by the parent element of the element currently using this .
My String is properly recovered, i tried to just display it and it return exactly what I want.
The problem is that if I just put the String in my url path like this :
<iron-ajax auto url="questions/{{path}}.json" handle-as="json" last-response="{{questions}}"></iron-ajax>
It doesn't work, I read on other threads that the cause of this is the use of a dynamic String which can't be used in the url path, as the String is data-binded.
If I wrote the url manually it works just fine :
<iron-ajax auto url="questions/listQuestions.json" handle-as="json" last-response="{{questions}}"></iron-ajax>
So I tried to compute my value to just return a String but it doesn't work either. It's has been hours of trying to come up with a solutions and research one on Internet but it just doesn't work.
Here's my code with the computed properties I tried :
properties: {
path :String,
url: {
type: String,
notify: true,
computed: 'computeurl(path)'
}
},
_acces: function(path) {
return "questions/"+path+".json";
},
computeurl: function(path) {
return path;
}
When I tried to display them like this :
<p><span>[[_acces(path)]] or [[url]] or [[path]]</span></p>
I got :
Display computed properties
If you want to use a bind to some property like this you need to use it with $ symbol.
Your example must have the view like this:
<iron-ajax auto url$="questions/{{path}}.json" handle-as="json" last-response="{{questions}}"></iron-ajax>
This ought to do you:
<template>
<iron-ajax auto
url="[[_computeUrl(path)]]"></iron-ajax>
</template>
<script>
...
properties: {
"path": {
type: String,
value: function() {
return 'listQuestions';
}
}
},
_computeUrl: function(path) {
return 'questions/' + path + '.json';
}
</script>
Update 2017-4-26: After reviewing your paste, you were missing the bower_component reference to iron-ajax; see pastebin here: https://pastebin.com/ShqzedyW. Also listed a couple of your properties that you hadn't named, made url a computed property. Check it out now.
this is the router code
Router.route('screens', {
path: '/screenshots/:_id',
template: 'screens',
onBeforeAction: function(){
Session.set( "currentRoute", "screens" );
Session.set("screenshots", this.params._id);
this.next();
}
});
this is the helper for screenshots template
Template.screens.helpers({
ss: function () {
var screenshots = Session.get("screenshots");
return Products.findOne({ _id: screenshots});
}
});
and am calling it here
<h4>Click to view the Screenshots
When i click to view the screenshots URL, the URL should be this /screenshots/:_id based on my router configuration, but what i see in the browser is /screenshots/ without the _id and the page shows 404 - NOT FOUND.
Is it possible to create nested routes?
because before i click on the link that executes the above route. i will be in this route
Router.route('itemDetails', {
path: '/item/:_id',
template: 'itemDetails',
onBeforeAction: function(){
Session.set( "currentRoute", "itemDetails" );
Session.set("itemId", this.params._id);
this.next();
}
});
and this route works fine i can see the item _id, is it possible to create another route inside it that has for example this path /item/:_id/screenshots?
I have the _id stored in Session.get("itemId"). Is it possible to call it in the path of the route somehow?
I tried '/item' + '/screenshots' + '/' + Session.get("itemId") but didn't work
or there is other way to solve it?
The problem is not with the code in the question, the 404 page is occurring due to it not being passed an id into the path, the browser says /screenshots/ and not /screenshots/randomId because it is only being passed that from the link.
As per additions to the question and chat with Behrouz: Because the value is stored in session we can use
Template.registerHelper('session',function(input){
return Session.get(input);
});
to register a global template helper called session which can be called with {{session session_var_name}} and create the link as below:
<h4>Click to view the Screenshots
I have this simply ajax/jquery call to a symfony2 controller/action
$.ajax({
type: 'post',
url: 'http://symfony.local:8080/app_dev.php/api/searches/guitar.json',
success: function(results) {
}
});
I'd need to change the first part of the url 'http://symfony.local:8080/app_dev.php/api/searches/guitar.json' in order to make it independent from the front controller I'm using. How can I achieve this?
First, why are you supplying the whole URL? You could omit the domain part so that the path is relative:
$.ajax({
type: 'post',
url: '/app_dev.php/api/searches/guitar.json',
success: function(results) {
}
});
Second, you can set up the prefix for your AJAX calls in a Symfony view:
<script type="text/javascript"></script>
{% if app.environment == 'dev' %}
var root = '/app_dev.php/';
{% else %}
var root = '/';
{% endif %}
</script>
I've introduced a window.root variable here. You can use it in your scripts then:
// ...
url: root + '/api/searches/guitar.json',
// ...
And as suggested in the comments, the best way here would be passing the URL to your scripts with a separate routing provider for JavaScript.
You could set the url as the href and catch it or set it as some of data tag.
As href
Your link
Link
And the javascript
$('a.ajax-link', function (e) {
e.preventDefault();
// Stop link updating page
var href = $(this).attr('href);
$.ajax({
type: 'post',
url: href,
....
});
});
Or as a data field
Your link
<a data-href="{{ path('your_route') }}" class="ajax-link">Link</a>
And the javascript
$('a.ajax-link', function (e) {
e.preventDefault();
// Stop link updating page
var href = $(this).data('href);
... see above ...
});
With either of these ways there is no use for FOSJsRouting or hard coded routes.
I added a new action to my controller, created the twig file and added the corresponding route to the routing.yml file. However I can't make it work. I keep getting:
Unable to find the controller for path /route/1/change
What am I missing?
# app/config/routing.yml
engineering_change:
pattern: /engineering/{id}/change
defaults: { controller: MgmtBundle:Engineering:change }
I generate the url in my template like this:
{{ path('engineering_change', { 'id': entities.id }) }}
It should read _controller instead of controller in your routing.yml.
-> Routing in Action
i changed my onlinesite,in online server,i created new module in schema.yml files.but the above error was comming.i remove catch file content.after that same error was comming.
Empty module and/or action after parsing the URL "/Planbook/planbook_new" (/)
#planbook
planbook:
url: /planbook
param: { module: planbook, action: index }
most_recent:
url: /planbook/most_recent
param: { module: planbook,action: mostrecent }
planbook_view:
url: /planbook/planbook_view
param: { module:planbook,action: view }
plan_new:
url: /planbook/plan_new
param: { module: planbook,action: new }
planbook_edit:
url: /planbook/planbook_edit
param: { module: planbook, action: edit }
highly_recommended:
url: /planbook/highly_recommended
param: { module: planbook,action: recommended }
mytrack:
url: /mytrack
param :{ module: planbook,action: mytrack }
plan_mate:
url: /planbook/plan_mate
param: { module: planbook, action: plan_mate }
#layer
layer:
url: /layer
param: { module: layer, action: index }
layer_new:
url: /layer/new
param: { module: layer, action: new }
layer_edit:
url: /layer/edit
param: { module: layer, action: edit }
layer_delete:
url: /layer/delete
param: { module: layer, action: delete }
layer_view:
url: /layer/view
param: { module: layer, action: view }
<a href="<?php echo url_for('/Planbook/planbook_new') ?>"
<a href="<?php echo url_for("#planbook_new") ?>"
hi plz help me.
Few things you must keep in mind
Make sure there are no cache files by using symfony cc or you can manually delete the contents of cache folder.
Make sure the database connection is configured properly in databases.yml as the login page may not require to be connected with database which is not the case regarding other pages.
Make sure that the rewrite_module has been enabled (I hope you are using wamp which makes it simple to enable it)
Make sure that cache and log folders have write permission (I don't think it will be an issue in a Windows machine)
You may also consider going through http://www.symfony-project.org/gentle-introduction/1_4/en/05-Configuring-Symfony
Thanks
Few things to perform:
check you error log
use your dev controller (something like frontend_dev.php) to see errors
and show us the error.
I don't think it's related to htaccess since you can display the login page.