I'm trying to find the best way to deal with dynamic routing generated through an AJAX call with Symfony2.
When a new call is made, I need the current path to be available , along with some dynamic variables that get passed into the path.
Essentially this .
A few answers have suggested putting the route into a variable within each templete , such as
<script type="text/javascript">
var productPath = {{ path("acme_myBundle_default_product" , {"magazine" : "bobscheese" , "product" : "chedderfornoobs"}) }};
</script>
The issue here is, the path rely s on variables, that won't exist at runtime (namely $magazine and $product).
A perfect solution would be FOSJsRoutingBundle it seems , but the installation doesn't seem to be up to date with the latest Symfony2 .
Installation runs fine with git submodule add git://github.com/FriendsOfSymfony/FOSJsRoutingBundle.git vendor/bundles/FOS/JsRoutingBundle
but then I think the rest of the ReadMe is out of date, following it gives me a blank screen, with no errors in the log.
So my question is , either , how to install FOSJsRoutingBundle in Symfony2.1.3 , or how best to handle client side generated URLS within Symfony2.
FOSJsRoutingBundle can be normally used in my environment(2.1.3).
Does routing go wrong?
Has it set up?
acme_myBundle_default_product:
pattern: // ...
defaults: // ...
options:
expose: true
I just went down the
<script type="text/javascript">
var basePath = 'http://www.mybaseurl.com';
</script>
Route. Not as fulfilling, but worked for me in this case.
Related
I'm using Kint via Composer in Laravel 4 by loading kint first in composer.json so that dd() is defined by kint, not laravel (suggested here).
I want to leave debug calls in my app, and disable Kint if not in the local environment. I'm successfully using config overrides for Anvard using the following structure:
/app/config/local/packages/provider/package_name/overridefile.php
Unfortunately, this is not working for Kint with the following structure:
/app/config/packages/raveren/kint/local/config.php or
/app/config/packages/raveren/kint/local/config.default.php
The Kint documentation states:
You can optionally copy the included config.default.php and rename to config.php to override default values…
…which works for me (/vendor/raveren/kint/config.php)
How do I achieve this:
without editing a file in the /vendor/ directory that will get overwritten by composer
so that kint is only enabled in the local envirnoment
I've also tried adding the following to a helpers.php file which is called before composer in /bootstrap/autoload.php as suggested here:
<?php
isset( $GLOBALS['_kint_settings'] ) or $GLOBALS['_kint_settings'] = array();
$_kintSettings = &$GLOBALS['_kint_settings'];
/** #var bool if set to false, kint will become silent, same as Kint::enabled(false) or Kint::$enabled = false */
$_kintSettings['enabled'] = false;
unset( $_kintSettings );
(but no dice :)
Any suggestions? TIA!
I'm not familiar with kint but checked the documentation and found that, to disable kint output, you may use (in runtime)
// to disable all output
Kint::enabled(false);
In Laravel you can check the environment using
$env = App::environment();
if($env == 'your_predefined_environment') {
Kint::enabled(false);
}
To configure your environment, you may check the documentation.
Update : I've setup my local environment as givel below (in bootstrap/start.php)
$env = $app->detectEnvironment(array(
'local' => array('*.dev'),
));
And in my local machine, I've setup a virtual mashine which has laravel4.dev as it's base url, so if I visit the app using laravel4.dev or laravel4.dev/logon then I can check the environment in my BaseController.php and it detects the local environment because of .dev
public function __construct()
{
if(App::environment() == 'local') {
// do something
}
}
In your case, I don't know where is the first debug/trace you used to print the output, so you should keep the environment checking and disabling the Kint code before you use any debug/trace but you may try this (if it works for you) but you can check the environment in your filter/routes files too.
Hmm.. I'm not sure if this is the ideal way to do it, but this works, and seems Laravel'ish:
// top of app/start/global.php
Kint::enabled(false);
and
// app/start/local.php
Kint::enabled(true);
(assuming you've got a local environment defined: see #TheAlpha's answer for more info)
http://laravel.com/docs/lifecycle#start-files
I'm trying to pass a javascript variable inside a dynamic url using Django. I have the following path
path('task-update/<str:pk>/', updateTask, name='task-update'),
I'm able to retrieve the "Task" fields I created (id, title, description, ...) depending on what task I select inside the HTML (this is done using AJAX and the Django-REST Framework). However, I'm having some trouble on rendering javascript values inside dynamic urls
var url = `{% url 'task-update' pk=${activeItem.id} %}`
The ${activeItem.id} is where I'm having some trouble, I tried assigning it to a variable and passing that into the URL but it doesn't work.
A workaround I've been using is
var url = `http://127.0.0.1:8000/task-update/${activeItem.id}/`
however I'd like to use django's template tags
After searching for quite a bit this was the best neat-looking solution I found (also the only one): django-js-urls.
Just pip install django-js-urls and add 'js_urls' to your INSTALLED APPS.
Afterwards add simply add JS_URLS to your settings.py file and put the names of the paths you'd like to use. In my case I only added task-update, it looks something like this
JS_URLS = (
'task-update',
)
Then, all you need to do is add the following in the URLs root module
from js_urls.views import JsUrlsView
urlpatterns = [
# other urls
url(r'^js-urls/$', JsUrlsView.as_view(), name='js_urls'),
]
And include the following js in the template
<script src="{% url 'js_urls' %}" type="text/javascript"></script>
URLs can be used using the window.reverse function
var url = window.reverse('task-update', { pk: activeItem.id });
I found a trick that might work under most circumstances:
var url = "{% url 'task-update' pk=12345 %}".replace(/12345/, ${activeItem.id});
It's clean, and it doesn't break DRY principle.
I have a Laravel 5.6 project with a JS file in the public directory.
I am looking for a way to get the route name within the javascript file. I need to set a variable in the javascript depending on the route.
Is there a way I can do this?
Just place a script tag on the view you want to access it from:
<script type="text/javascript">
var token='{{csrf_token()}}' ;
var AuthCheck='{{Auth::check()}}';
var currentRoute='{{Route::current()}}'
</script>
I'm using jquery.cookie v1.4.1 to set a cookie like this
$.cookie('userCookie', $("#user").val());
Where $("#user").val() is returning something like 'username'
Then in an angular app, am trying to retrieve this cookie using
var userCookie = $cookies.get('userCookie');
But is not working, I'm getting:
var userCookie = undefined
I'm using AngularJS v1.4.8 with corresponding ngCookies
Any help would be nice....
Thanks to charlietfl I got it to work.
It happens that I was setting the cookie for a specific path, let say '/domain/somepath' and then I was trying to retrieve it from '/domain/someotherpath', where the cookie was not available.
I fix it specifying the path on the cookie to all my domain like this:
$.cookie('userCookie',$("#user").val(), { path: '/' });
Why don't you use angular to store your cookies?
$cookies.put('userCookie', $("#user").val());
Btw: its horrible if you use JQuery like this inside your Angular App.
$cookies.get('userCookie');
Your problem maybe: the cookie will be created BEFORE your DOM-Element is even rendered on the page. Try $.cookie('userCookie', 'test123'); , this should work pretty sure (with angular 100%).
How to fix this? First of all, DONT USE JQUERY LIKE THIS IF YOU HAVE ANGULAR!!!
<input ng-model="username"></input>
$cookies.put('userCookie', $scope.username);
I am working on the basic template of Yii2. I have got a jQuery script views/usuario/js/create.js that it's only going to be used in one view views/usuario/create.php.
I'd prefer not to use ...
public $jsOptions = array(
'position' => \yii\web\View::POS_HEAD
);
... in assets/AppAsset.php in order to mantain loading of scripts at the end of the page.
create.js it's only needed in the create.php view so I'd prefer to load it just for the create.php view.
So I've tried unsuccessfuly to follow ippi's instructions
Firebug keeps throwing me this error:
SyntaxError: expected expression, got '<'
http://www.example.com/usuario/js/create.js
Line 1
I guess there could be a problem with the route param of ...
$this->registerJsFile('js/create.js');
... but I can't find it out.
Any help would be appreciated.
registerJsFile() needs an url, you should simply publish (make it web accessible) your file before registering it, e.g. :
$pub = Yii::$app->assetManager->publish(__DIR__ . '/create.js');
$this->registerJsFile($pub[1], ['depends' => ['yii\web\JqueryAsset']]);
Or you could create an asset bundle and registering it in your view.
Read more about publish() and registerJsFile().
Try to use $this->registerScriptFile('views/usuario/js/create.js');