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>
Related
I am developping an asp.net web application :
In the website folder, I have a folder called "integrations" containing a list of js files (name1.js, name2.js, name3.js ...)
A user makes a http request to a mvc controller method where he gives as input a "name".
This method gives the "name" as a property of a viewmodel to a razor view containing the following code :
This razor view is returned to the user.
The previous code is working well, but I would like to add an improvment. Actually you need to add a js file inside the folder integrations,
and you cannot do that when the application is running in production.
So I would like instead of having a script tag referencing a file placed inside the integrations folder to have a script tag containing a js content
coming from a data table like this :
name : jscontent
name1 : jscontent1
name2 : jscontent2
But I don't know how to changed this :
<script src="~/integrations/#(Model.Name).js"></script>
To :
<script>>query in db by #(Model.Name) parameter to get corresponding jscontent</script>
Make it a controller/method and you can take advantage of things like caching, auth etc for free:
public class ScriptController
{
public ActionResult Content(MyModel model)
{
var result = "alert("Hello World!");";
return JavaScript(result);
}
}
JavaScriptResult
Then in your html:
<script src="~/Script/Content?prop1=whatever"></script>
I need to access the session value in external js file. I have tried,
var userid = '#HttpContext.Current.Session["UserID"]';
this dosen't work as the variable userid take the right hand side as a string. Can anyone suggest me a idea.
You can access the Session variable in .cshtml and pass it to external javascript file by calling function of external .js file.
var userid = '#Session["UserID"]';
externalJsFunction(userid);
You can assign session to some hidden field and get the hidden field value (session value) in document.ready in external js file.
I have one solution for you.
In layout of your website define any empty div and set its data attribute value with session value if you are using html5 as,
<div id="sessionDiv" data-id="#Session["UserID"]"></div>
Now in your external js file access this div.
var userid=$('#sessionDiv').attr('data-id');
You could add a script tag at the top of the .cshtml file and assign a the userid to a global variable. This variable is then accessible in all javascript files.
index.cshtml
...
<script>
document.mynamespace = {};
document.mynamespace.userid = '#HttpContext.Current.Session["UserID"]';
</script>
....
javascript.js
var userid = document.mynamespace.userid; //do something with userid
mynamespace is a proxy object so that you dont pollute the global namespace.
I am developing asp.net mvc project. I have a javascript object that sends query to controller action. in cshtml file I can use #Url.Action("get", "product") so when I publish the web site, url action is rendering by location url. If I publish it http://localhost/App1/, the action url is like this http://localhost/App1/product/get or I can publish it another directory like http://localhost/App2/ and so on.
But I have an issue in javascript code.
sample.js
function query(){
var url = "/product/get";
// send query this url
}
When I publish the project in http://localhost/App1/ url (APP1 folder), javascript query is sending request to http://localhost/product/get , but it should be like this http://localhost/App1/product/get
I can not use #Url.action() razor expression. How can I solve this issue?
You can place root level appPath variable in your _layout.cshtml
<script type="text/javascript">
var appPath = #Url.Content("~/");
</script>
Then in your query
function query(){
var url = appPath +"/product/get";
}
I send the url as a parameter from the View to the javascript function which is written in a js file. This saves you the trouble of creating a url in js and you can use you razor expression for the same.
So your function becomes:-
function query(url){
//var url = "/product/get";
// send query this url
}
Im trying to use PHP to generate a javascript file. I have the file included and all scripts are workning in it. The think i cant figure out is how i pass a parameter to this file?
To pass a parameter from the controller to the view i use:
$this->set('object_models', $object_models);
To pass it from the view to a element i use:
echo $this->element('pageElement', array('object_model' => $object_model));
Im including my js.php file by adding it in the view with:
echo $this->Html->script('modelDrawer.js.php?', false);
Have you considered using parseExtensions and including your JS file as a view for a controller that you can perform logic on directly?
This would work similarly to the way rss feeds and xml files are generated with Cake.
See this article
UPDATE
Go to your routes.php file, and add the line Router::parseExtensions('js');
Then, create a controller called, for the sake of this, DynamicController.php - and paste this in there:
class DynamicController extends AppController {
public $uses = array();
public function modelDrawer() {
// logic in here
$this->set( 'object_models', $object_models );
}
}
Create a view folder and view file:
/app/View/Dynamic/js/model_drawer.ctp
In that model_drawer.ctp file, you can place your view/script logic that you want to be cakeified.
You can then call your script like this:
<script type="text/javascript" src="/dynamic/modelDrawer.js"></script>
Give that a try!
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.