How to add page specific JavaScript files to the Laravel Assets Pipeline? - laravel

I using Laravel Assets Pipeline(https://github.com/CodeSleeve/asset-pipeline) plugin for the my project.
I was able to successfully configure with the common JS files (jquery.js, global.js) included in the page. I have some page specific JavaScript files that needs to be included.
home.js
about-us.js
I tried with following but I think it's not the correct way to achieve this.
{{ HTML::script('assets/home.js'); }}

According to the asset-pipeline documentation it should be used like that :
<?= stylesheet_link_tag() ?>
<?= javascript_include_tag() ?>
There is a manifestFile argument which can maybe load only some specific files.
Are you sure it is compatible with HTML facade ? If it is, there is maybe a route to configure :/
And as far as I understand the documentation, you shouldn't call your script like that, one of the aim of your asset pipeline is to delegate it, isn't it ?

Related

Laravel - how to access public folder from JS file

I am using the exact same Ajax call in multiple blade templates.
If Ajax fails, I show an error jpg located in public/images/danger.png.
When I had my JS in the blade file written inline in <script> tags, it was easily accessible as I had blade helpers to access public with:
{{asset('/images/danger.png')}}
Yet now, accessing /public with:
http://"+location.host +"/public/images/danger.png
is impossible.
It would be unDRY to put the exact same AJAX call in 12 blades, no need to elaborate why..
How do you guys counter this issue?
remove /public from your route,you can directly use /images/imgname.jpg
You can just use absolute routes in your javascript files. This will however not work if your Laravel application sits in a sub folder.
"/public/images/danger.png"
just the same as used in html (in blade)
like
$('.abc').attr("src", "{{asset('imgs/male_b.svg')}}");

Writing JS code in blade templating engine

I have a Laravel 5.0 project and inside of that I have a foo.blade.php file,in here I want to use some Javascript to get the pathname of the link the user currently in like so
window.location.pathname
Is it possible? Or blade templating allows only php to be written on the file?
First, Javascript runs client side, so whatever you write in <script> tag will be totally ignored by laravel.
But if you want to access path (route) in laravel, you can check this question.

Can Laravel Blade be used in a sandbox environment?

Can Laravel Blade be used in a sandbox environment, similar to Twig's sandbox extension?
I have the need to allow users to use a template system but obviously do not want them executing arbitrary PHP code on the server.
I would like to use Blade since it's already part of Laravel but suspect this isn't possible.
The short answer is no, by default.
You could probably implement this yourself however, by extend blade to add custom functionality. See the Blade Documentation.

External PHP Include in Laravel

We run a main website which is not made in Laravel. A specific script on this website however is made in Laravel. We need this script (or to be exact, a specific view inside of it) to fetch some resources from the main website (PHP files which mainly include HTML). When, inside the Laravel application, we try to include these resources, we can not - as they don't exist in the Laravel project workspace. This results in an error that the file does not exist. Attempting to climb out of the project (../../../file.php) does not seem to help either.
We're including it this way:
<?php
include '~/template/nav.php';
?>
We don't wish to include this file into the actual Laravel project, as that would require having to update it twice to ensure they remain equal. Is there any way for us to include this "external" file in our view? Every bit of research done seems to suggest adding it into the project, which would just cause twice the amount of administration on updates.
Cheers!
Just faced same problem. My Laravel Project is an API that need's to include outside php file. So what I did was specify entire path:
include($_SERVER['DOCUMENT_ROOT'].'/../../my_example_file.php');
$_SERVER['DOCUMENT_ROOT'] will lead you to Laravel's public folder.

Codeigniter anchor can't find link

I am using CodeIgniter to design a website, and I am having trouble getting anchor to work correctly.
Here is how I am using it. It is used in a file in views folder of application in codeigniter.
<?php echo anchor('/profile', 'PROFILE'); ?>
My intention is to have anchor load the profile controller, which is in the controllers of application in codeigniter. However, when I clink on the link, it says that the the file is not found.
I autoload the url helper functions alreader, and other functions from that file are working, like site_url() .
I don't know what I could have missed? Do you ave any suggestions? This is my second project with codeigniter, so I am still learning.
EDIT: Yes, I am following the naming convention in codeigniter, and the file has an index function. I tried without the forward slash and it still gives the same result.
The HTML link that this produces is localhost/profile. This is what I Should get right? Since for codeigniter, it's url/controller/function. I did a mod rewrite to remove index.php from the url, but that shouldn't be a problem, should it? I'll try and check the base url again.
Are you sure you need the leading forward slash?
Have you tried:
<?php echo anchor('profile', 'PROFILE'); ?>
You would only be using the slash if you are trying to get into the "profile" sub folder inside your controllers folder.
What helps me when urls get confusing is to type into the browser the full url to the path I want to get to (to make sure I'm not getting a 404, etc). From there you can start from the end and go back to see what you are missing.
But you generally want to start with a controller name and add the function you are calling like:
anchor(controllerName/functionName)
See CodeIgniter URL Helper for examples.
EDIT: also - you may need to check what your config base_url is set to. if there's fancy .htaccess stuff happening, your base_url needs to be set properly.
There is a very simple solution go to config/constants and define SITE_URL there and set its value to
http://localhost/myprojectname/index.php.
Then use it here like this.
<?php echo anchor(SITE_URL.'/profile', 'PROFILE'); ?>

Resources