I setup the following Routes for now (Moving a barebone php project to laravel):
Route::get('/', function () {
return view('index');
});
Route::get('cat/a', function (){
return view('cat.cat_a');
});
Both Files include 4 other Files located in /resources/views/includes. I load those files using:
#include('includes.header')
#include('includes.sidebar_a')
[...] Content [...]
#include('includes.sidebar_b')
#include('includes.footer')
All includes are correctly included in both, domain.com/ aswell as domain.com/cat/a but whitin the view domain.com/cat/a all included css & js files (included in footer.blade.php & header.blade.php) return 404.
Here is my file structure:
You should use asset helper to generate links to JS&CSS:
{{ asset('js/main.js') }}
In this case you'll get HTML with correct paths.
Related
I am using XAMP localhost. I have a folder named wesite in my htdocs for my website template I am creating. My ajax call works fine when both js and php is in the root of this folder. However, when I create a folder name functions and put the js and php script there at the root, the js ajax call don't call the php. Below is the code. It works fine when both are in the directory as the index.php or any other html files, but not in a sub directory. I know it has to do with the path because the scripts work otherwise.
The folder structure is like this there is a folder called Website which has all the html files saved with php ext. Inside this Website folder I have the functions folder which has all the js and php functions no sub directory in the functions folder so all js and php files are at the root of the functions folder. When I look at the inspect element in Chrome, it says the update.php file has a 404 file not found error. This is weird giving the fact that both the js and php are in the same folder. The php script is a simple test script which just echo "Hello From php" to see if I am getting a response
function navUpdate(){
alert('Hello from functins');
var navItemOne = document.getElementById('menu-item-one').innerHTML;
var navItemTwo = document.getElementById('menu-item-two').innerHTML;
var navItemThree = document.getElementById('menu-item-three').innerHTML;
var navItemFour = document.getElementById('menu-item-four').innerHTML;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
alert(this.responseText);
}
}
xhttp.open("POST", "update.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(`nav-item-one=${navItemOne}&nav-item-two=${navItemTwo}&nav-item-
three=${navItemThree}&nav-item-four=${navItemFour}`);
}
this is in app directory
'locale' => 'en',
this is in web.php
Route::get('/{locale?}',function($locale=null){
return view('index');
});
Route::get('/about/{lang?}',function($lang=null){
App::setlocale($lang);
return view('about');
});
Route::get('/contact/{lang?}',function($lang=null){
App::setlocale($lang);
return view('contact');
});
// Route::get('/portfolio/{lang?}',function($lang=null){
// App::setlocale($lang);
// return view('portfolio');
// });
I have commented out the portfolio route but it is still working. if i delete this rout it still works
My app's default language is English. but when I setlocale to en or bn images are not showing correctly.{except index route}
http://127.0.0.1:8000/about view
http://127.0.0.1:8000/about/en view
A file path describes the location of a file in a web site's folder structure.
<img src="images/illustrations/leaf-bg-top.png" alt="illustrations">
That means the "images/illustrations/leaf-bg-top.png" file is located in the same folder as the current page. If you give a slash (/) before the path, :
<img src="/images/illustrations/leaf-bg-top.png" alt="illustrations">
That means, images/illustrations/leaf-bg-top.png file is located in the images folder at the root of the current web
I am a beginner in laravel and have made one frontend page as blade.php
But not getting how to see the output on browser.
Is there any other code which I have to add.
If your blade file has name resources/views/index.blade.php, you need to add this code to file routes/web.php:
Route::get('/', function () {
return view('index');
});
How can I access the images stored inside public/images folder without the use of Laravel's own functions like assets()?
I just want to get the direct URL of the images stored in images folder.
Eg:
localhost/webapp/images/stackoverflow.png
When requesting the link i should get the direct image.
If you are inside a blade template
{{ URL::to('/') }}/images/stackoverflow.png
Just put your Images in Public Directory (public/...folder or direct images).
Public directory is by default rendered by laravel application.
Let's suppose I stored images in public/images/myimage.jpg.
Then in your HTML view, page like: (image.blade.php)
<img src="{{url('/images/myimage.jpg')}}" alt="Image"/>
I have created an Asset helper of my own.
First I defined the asset types and path in app/config/assets.php:
return array(
/*
|--------------------------------------------------------------------------
| Assets paths
|--------------------------------------------------------------------------
|
| Location of all application assets, relative to the public folder,
| may be used together with absolute paths or with URLs.
|
*/
'images' => '/storage/images',
'css' => '/assets/css',
'img' => '/assets/img',
'js' => '/assets/js'
);
Then the actual Asset class:
class Asset
{
private static function getUrl($type, $file)
{
return URL::to(Config::get('assets.' . $type) . '/' . $file);
}
public static function css($file)
{
return self::getUrl('css', $file);
}
public static function img($file)
{
return self::getUrl('img', $file);
}
public static function js($file)
{
return self::getUrl('js', $file);
}
}
So in my view I can do this to show an image:
{{ HTML::image(Asset::img('logo.png'), "My logo")) }}
Or like this to implement a Java script:
{{ HTML::script(Asset::js('my_script.js')) }}
when you want to access images which are in public/images folder and if you want to access it without using laravel functions,
use as follows:
<img src={{url('/images/photo.type')}} width="" height="" alt=""/>
This works fine.
You simply need to use the asset helper function in Laravel. (The url helper can also be used in this manner)
<img src="{{ asset('images/arrow.gif') }}" />
For the absolute path, you use public_path instead.
<p>Absolute path: {{ public_path('images/arrow.gif') }}</p>
If you are providing the URL to a public image from a controller (backend) you can use asset as well, or secure_asset if you want HTTPS. eg:
$url = asset('images/arrow.gif'); # http://example.com/assets/images/arrow.gif
$secure_url = secure_asset('images/arrow.gif'); # https://example.com/assets/images/arrow.gif
return $secure_url;
Lastly, if you want to go directly to the image on a given route you can redirect to it:
return \Redirect::to($secure_url);
More Laravel helper functions can be found here
In my case it worked perfectly
<img style="border-radius: 50%;height: 50px;width: 80px;" src="<?php echo asset("storage/TeacherImages/{$teacher->profilePic}")?>">
this is used to display image from folder i hope this will help someone looking for this type of code
Crete file .htaccess in root && add
...
RewriteCond %{REQUEST_URI} !(.css|.js|.png|.jpg|.gif|robots.txt|.ttf)$ [NC]
...
I am new to CI and i just want to set the default template page, I am now using a library called Eztemplatei tried to load the view by using the following code:
`$this->template
->set('title','Home')
->load('view','index')
->render();`
but it's not working and it seems that all the files haven't been loaded. Any help would be much appreciated
First, you have to make sure your css and js files directories are correct
'asset_path' => 'assets/urApp/',
// ex: assets/ -> http://www.yourwebsite.com/codeigniter/base/url/assets/
'asset_css_folder' => 'css/', // is added to asset_path
'asset_js_folder' => 'js/', // include trailing slash
'asset_views_folder' => 'views/', // include trailing slash
second, remember once you load some view you can't render again.
so your code should be sth like:
$this->template
->set('title','Home')
->set('view','index')
->render();