I have blade file named index-game.blade.php. And in one line of it, a line that reaches a js file under laravel public folder.
<script type="text/javascript" src="{{asset('game/')}}/js/CPreloader.js"></script>
So far so good.
Also inside CPreloader.js, I have lines like this.
s_oSpriteLibrary.addSprite("bg_menu","./sprites/bg_menu.jpg");
s_oSpriteLibrary.addSprite("progress_bar","./sprites/progress_bar.png");
At this stage, I get the following error in the console of my browser's devtools screen:
bg_menu.jpg:1 GET http://127.0.0.1:8000/games/sprites/bg_menu.jpg 404 (Not Found)
progress_bar.png:1 GET http://127.0.0.1:8000/games/sprites/progress_bar.png 404 (Not Found)
This is the link where I called "index-game.blade.php" => http://127.0.0.1:8000/games/slot1 (Let's pay attention to the games word here)
It works if i change my javascript file to
s_oSpriteLibrary.addSprite("bg_menu","../game/sprites/bg_menu.jpg");
s_oSpriteLibrary.addSprite("progress_bar","../game/sprites/progress_bar.png");
Because the correct link is this instead
http://127.0.0.1:8000/games/sprites/bg_menu.jpg // wrong
http://127.0.0.1:8000/game/sprites/bg_menu.jpg // true
I don't want to change the paths in all my javascript files. Because this is a game and there are as many pictures as possible. How do I correctly identify the laravel public folder in my external js files.
There is a package for that: https://github.com/tighten/ziggy
Some copy/paste from docs:
Install Ziggy into your Laravel app with
composer require tightenco/ziggy
Generate javascript route file:
php artisan ziggy:generate
Import in js file and use:
// app.js
import route from 'ziggy';
import { Ziggy } from './ziggy';
// ...
route('home', undefined, undefined, Ziggy);
Related
I am working on a Laravel project using ckeditor. All was working fine with the editor and no errors in the console until I ran:
php artisan make:auth
The project login/register then worked but the editor was not displaying with the error in the console:
Uncaught TypeError: a.ui.space(...) is null
I tracked it down to the line added to the app.blade.php file during the auth build:
<script src="{{ asset('js/app.js') }}" defer></script>
If I remove defer the editor comes back but I now get the console error:
[Vue warn]: Cannot find element: #app
I have seen quite a number of posts regarding the editor error but they not helping me get the root of the issue. Any ideas how to fix the error correctly?
I think the problem is that the script is executed before the DOM element is loaded in the DOM. Or you've placed it before the target element and then when the script calls, it cant find the element.
So you could place the script in a load event handler, something like this:
window.onload = function () {
// your script here
}
window.addEventListener('load', function () {
// your script here
}
for a real time project i use laravel 8 and axios javascript library used to make HTTP requests.
the backend work great .
for example if go to this URL http://127.0.0.1:8000/api/usersthe following data will be show
[{"id":1,"name":"Hossein Khosromanesh","email":"test#gmail.com","email_verified_at":null,"created_at":"2022-01-22T19:52:12.000000Z","updated_at":"2022-01-22T19:52:12.000000Z"}]
the problem comes when i want to show the data to the frontend , we got error Uncaught TypeError: Cannot read properties of undefined (reading 'get') at users:84:18
actually get('/api/users') from window.axios.get('/api/users') is the main problem
the front end code :
<script>
window.axios.get('/api/users')
.then((response) => {
const usersElement = document.getElementById('users')
let users = response.data
users.ForEach((user,index) => {
let element = document.createElement('li')
element.setAttribute('id' , user.id)
element.innerText = user.name
usersElement.appendChild(element)
})
})
</script>
what's wrong was my code ? can somebody please help me and free me from darkness : )
If you are importing axios using a script tag in your html or blade file, you can use it with axios.get instead of window.axios.get.
As per My understanding from your code. You have to import axios in vue component or view where you consume API like below.
import axios from "axios";
Then You can use it like
axios.get('/api/users').then();
Note:- No need to register axios inside windows object in app.js for more information please refer below link
https://axios-http.com/docs/intro
If above method still not work for you then follow below step
Take backup of your code first then perform below step
Check your package.json file and verify axios is present over there or not.
If you don't find then install it using npm command
If you find then delete your package.lock.json file and run npm install command
Then follow import step mentioned above and try.
I was watching a tutorial that uses laravel and vue and on the tutorial he add a vue component in the components folder and then register that component in app.js file using this code
Vue.component('articles',require('./components/articles.vue');
And it works fine and in my case when I use this code it says require is not defined.
In the app.js file theres already an example component registered using this code
Vue.component('example-component',__webpack_require__(37));
My question is how does the number 37 define the ExampleComponent.vue file? And how can I get the number of the file i want to include? And I don't have the webpack CLI
Thanks
Its looks like i have been using the wrong app.js file which was in the public directory i should be adding these codes to the app.js which sits in the components directory
You should try
import YourComponent from 'path';
new Vue({
components: {
"your-component": YourComponent
});
I have a challenge: I can't get a blade file working as expected following the Laravel guide for such a purpose.
I know a Laravel blade file must end with the .blade.php.
Every time I create a blade file, I get an error as the file cannot be found, but that is not the case with a pure PHP file.
Below is my route definition to return my blade test file, testblade.blade.php:
Route::get('/test', function () {
return view('testblade');
});
When returning a pure PHP file in my route file, testphpalone.php, I define my route as follow:
Route::get('/test', function () {
return view('testphpalone');
});
I'm concerned because I cannot use blade helper functions such as #yield() and #extends() directly in the pure PHP file, but a blade one (which I can't get working).
What am I doing wrong and how can I make it work?
Taking into consideration your route definition;
Route::get('/test', function () {
return view('testblade');
});
your blade template file should be testblade.blade.php and located in your resources/views directory as in: resources/views/testblade.blade.php for a proper implementation.
you only can use blade syntax in a blade file. As a best practice always name view files using .blade.php syntax.
find more on blade syntax here
In config\view.php check the path of the views
'paths' => [
realpath(base_path('resources/views')),
]
I had this problem and it was the server, I was using the XAMPP. How I found out is that I ran my project on an online server and it worked perfectly.
The Solution is just to run your project using the command:
php artisan serve
then browse your project using:
localhost:8000
I'm trying to use javascriptRoutes in Play 2 (Scala) and I am getting an error (see below). Here is what I did:
Add javascriptRoutes method to Application controller
def javascriptRoutes = Action { implicit request =>
import routes.javascript._
Ok(Routes.javascriptRouter("jsRoutes")(Orders.searchProducts))
.as("text/javascript")
}
Add route to routes file
GET /assets/javascripts/routes controllers.Application.javascriptRoutes
Add <script> import to main.scala.html
<head>
...
<script type="text/javascript" src="#routes.Application.javascriptRoutes"></script>
...
</head>
With these changes in place I am getting the following error in the JavaScript console:
GET http://localhost:9000/assets/javascripts/routes 404 (Not Found)
Uncaught ReferenceError: jsRoutes is not defined
What am I missing?
wrong order within the conf/routes file may cause the issue.
there is a hint:
http://grokbase.com/t/gg/play-framework/1348rbs2vk/2-1-scala-javascript-router-404
once I adjusted the order according to the hint:
Move the route definition of the javascript router action above the
assets route.
the issue was fixed.
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
GET / controllers.MainController.index()
GET /message controllers.MessageController.getMessage()
GET /assets/javascripts/routes controllers.MessageController.javascriptRoutes()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /webjars/*file controllers.WebJarAssets.at(file)`
In the meantime I have found an other post related to this ->
Unable to resolve reverse routing methods in IntelliJ
I had to remove some info from the project config to get the following folders to become part of source route ->
File -> Project Structure
Select Sources in Right Pane
Add Source folder
target/scala-XXX/classes_managed
target/scala-XXX/src_managed/main