Yii is not auto including jquery - php

I have a strange problem with Yii & Jquery:
When I open a page which uses ajax/jquery on localhost Yii does automatically add asset include calls to the of the page:
<link rel="stylesheet" type="text/css" href="/PATH/assets/f72b359d/style.css" />
<script type="text/javascript" src="/PATH/assets/2e442e1a/jquery.js"></script>
<script type="text/javascript" src="/PATH/assets/2e442e1a/jquery.cookie.js"></script>
However when I run same code on the server Yii does not do it hense no Jquery available.
I'd appreciate if someone could direct to a way to solve it.

It seems like you're not registering jQuery in the correct way. You must add the following line in /protected/views/layout/main.php before </head> tag:
<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>
It will load jQuery automatically. Remember, if the YII_DEBUG flag is on it will load non-minified version, when deactivated (on production) it will load minified version. Hope this will fix your problem.

If all is working on your local machine it might be a permission problems
Have you checked that /assets is writable on your server ?
Yii publishes the assets it needs at runtime in this directory

Related

Symfony 4 + ReactJS Not Working Without Any Error

I am trying to add google map search capability for my project and i found a very nice react code for this (https://github.com/ubilabs/react-geosuggest) but i couldn't add this to symfony 4 based admin panel.
To understand what is wrong i created a new symfony 4 project and just added one controller and a twig template.
Generally i followed the following tutorials
https://www.thinktocode.com/2018/06/21/symfony-4-and-reactjs/ and
https://symfony.com/doc/current/setup.html
But when i ran the example i got the following result
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome!</title>
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="/build/app.js"></script>
</body>
</html>
As you can see, it is just the same code with the index.html.twig; there is no change. React didn't change anything instead of i expected.
This tutorial is not the one i just tried.Also I tried, React Getting Started tutorial and other Symfony 4 + React examples but the result was always same.
You can access the source codes from here:
https://github.com/bahadirarslan/Symfony4ReactProblem
UPDATE
I also tried steps described here https://artemzhuravlev.com/blog/symfony-reactjs-using-encore/ but couldn't make it.
The only thing i want to mention, when i try to run npm install i always get EACCES: permission denied error so i am running npm install with sudo and --allow-root attributes like described here https://www.fahidjavid.com/fix-error-eacces-permission-denied-mkdir/
Finally i found the solution with helps Vincent Le Biannic (https://github.com/Lyrkan) at github.
He took my attention to lines below at webpack.config.js
// will require an extra script tag for runtime.
// but, you probably want this, unless you're building a single-page app
.enableSingleRuntimeChunk()
This config make necessary to call runtime.js (which was generated by encore) in html file.
But adding only runtime.js didn't help so i also added vendors-app.js and voila!
Interestingly neither symfony's original documentation nor any tutorial about this subject don't contain any single line about this situation.
If you see an EACCES error when you try to install a package globally, you can either:
Reinstall npm with a node version manager (recommended),
or
Manually change npm’s default directory
Source : https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
The same problem for me
I added the line the twig header
<script type="text/javascript" src="{{ asset('build/vendor/app.js') }}"></script>
and it works

Content Not Loading Via SSL

I have a banner in which the HTML is loaded into my file via:
<?php require("banner.php"); ?>
When I attempt to load my page via SSL (https://vantagewebservices.com/) the banner does not load. Chrome and FF are warning me that some of my scripts are not being loaded over SSL. However, the links in the HTML are all dynamic in the way that they are link via the root of my website and not by the domain. The banner.html file that is loaded into my index page can be found here: vantagewebservices.com/banner.html. If you can help me resolve this issue I would greatly appreciate it.
Some of your scripts are not loading via https, change the following lines to be protocol relative as mentioned by #Ennui below.
<link href='//fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
The main problem is that JQuery was not loading which your banner is dependent on.

How to include JS/CSS files into templates of Slim Framework?

I am developing a simple web app with Slim framework. I got stuck with a probably simple problem. I want to include static files (CSS and Javascript) into my template.
My project folder structure is as follows:
index.php //<=== where all the routing happens.
/flot
layout.css
jquery.js
....
/templates
first_template.php
My header of first_template.php contains:
<link href="../flot/layout.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript" src="../flot/jquery.js"></script>
When I call the projects root url
http://localhost/xampp/ProjectX/
(I'm using XAMPP) the template shows up, but the css and javascript stuff is not working. The Google Chrome console shows:
GET http://localhost/xampp/ProjectX/flot/layout.css 404 (Not Found)
GET http://localhost/xampp/ProjectX/flot/jquery.js 404 (Not Found)
Any suggestions? I spent almonst one hour in googling, but the overall documentation of the Slim framework is still literally slim :)
I assume you are displaying your Template on the Project root (index.php), so you should remove the ../ from your Stylesheet and JS relative Paths:
<link href="flot/layout.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript" src="flot/jquery.js"></script>

Joomla 2.5 not loading JS scripts through article?

In Joomla 2.5 I have installed DirectPHP, which I have used extensively in Joomla 1.6 to embed PHP in Joomla articles. It works very well. I usually just for one include in the article, like this:
<?php
include "my_cool_script.php";
?>
Then I make my code in my_cool_script.php. One thing I often do is include more JS or CSS scripts in the page, right through the article, but putting the following lines in my_cool_script.php:
<link rel="stylesheet" type="text/css" media="screen" href="/scripts/css/dark-hive/jquery-ui-1.8.21.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/scripts/css/ui.jqgrid.css" />
<script src="/scripts/js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="/scripts/js/jquery-ui-1.8.21.custom.min.js" type="text/javascript"></script>
<script src="/scripts/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="/scripts/js/jquery.jqGrid.src.js" type="text/javascript"></script>
This has always worked in the past, with Joomla 1.6. Now in the Joomla 2.5 install. The rest of the PHP and HTML is loading in the article, and there aren't any errors on the page as far as loading the scripts. I look at the HTML code and I can see the above lines written to the page, but I cannot access any of the jQuery functions, like the script was not loaded. I get an error when I try to use any jQuery syntax. I know this script was working, because I just moved it from another site, running joomla 1.5 to the new one, 2.5.
Does anyone know anything about why the scripts aren't loading?
EDIT
Well i guess the script was loading, but it didn't like that used the $ symbol. I changed them all to jQuery and it works.
I am not sure exactly why, but perhaps in my installation there is some conflict, and the $ symbol is being recognized as it usually does, signifying that we are dong jQuery. So I changed all the $ to jQuery in my PHP scripts and all the jQuery seems to be working now.
Mootools (Joomla's javascript default framework until version 2.5) also uses '$', so it's very easy to run into some complications if you're unaware you're running scripts relying on both frameworks side to side.
Having said that, you could use 'jQuery' instead of '$' and release '$' for everyone else using the jQuery.noConflict(); command.
Cheers.

Symfony Managing Assets directly

Sorry for opening another topic on this but after hours of researching I haven't found a solution:
As stated in symfony's documentation I want to include my assets (css, js, images) directly and without using Assetic. The documentation is recommending the following sniplet:
<script src="{{ asset('js/script.js') }}" type="text/javascript" />
Unfortunately this always ends in a 404 file not found error for every try. I have placed my assets in the myBundle/Resources/public folder and I also copyied it into the web folder using php app/console assets:install web without success.
What am I missing here? Is there a configuration missing? Thank you in advance.
asset('js/script.js') will produce a link like /js/script.js. The files will be copied to a bundles folder, not to the web root. You want something like this to generate the correct URL:
<script src="{{ asset('bundles/myBundle/js/script.js') }}" type="text/javascript"></script>
(Note also that script tags need to have a closing </script> to make them work cross-browser.)

Categories