Image path issue when uploading Laravel 6 project to live server? - laravel

I uploaded my Laravel 6 blog to a live server. I put the content of the 'public' directory in the "public_html" folder and I created another directory outside the "public_html" where I uploaded the rest of the files and folders.
Everything seems to be working ok, except when I create a new category or post the images are not uploaded. I guess it's a path problem.
I think I need to modify the path in the controller, like here
$category = Image::make($image)->resize(1920,479)->stream();
Storage::disk('public')->put('category/' . $imagename, $category);
Since I no longer have a directory called "public", what should I put there? How do I change this path?
My css and js files were linked like this on localhost:
<link href="{{ asset('public/assets/frontend/css/bootstrap.css') }}" rel="stylesheet">
when I just left out "public/" they were working fine on the live, but if I leave out the "public" in the controller code, it's not working.

Related

Asset not loading, css, js or even images are not loading or linking in my Laravel 7 project

I don't know how else to go about this issue. I've tried several suggestions I've seen online but to no avail. I'm working with laravel 7
My view is not loading any file from my public folder. Even images uploaded from the view to the directory is not loading back to view when called. I've symlink the storage and the public directory. However, I observed that my storage folder created was a shortcut but I expect that should be correct.
These are different ways I can remember I tried to call an image from the directory for example
<img src="{{ asset('storage/cover_images/img1.jpg') }}" alt="">
<img src="{{ URL::asset('storage/cover_images/img1.jpg') }}" alt="">
<img src="{{ Storage::url('cover_images/img1.jpg') }}" alt="">
<img src="/public/cover_images/img1.jpg">
Also note that all CDNs or images referenced externally are working properly.
When you symlink the storage public disc, a shortcut directory storage is created in your Laravel project's public folder. Now the asset() helper of Laravel isn't restricted to access storage files only, instead you can use this to access any file on the public path. By default, asset() helper uses the public directory as the root.
For example, if you have uploaded an image to public/images/first.png, then you can use asset('images/first.png') to generate the full URL for the image.
If you have properly symlinked the storage disk then you should be seeing files in the newly created public/storage directory. If it doesn't contain any files, you must have symlinked it improperly.
To ensure that asset() helper works fine, create a directory foo in your Laravel project's public folder and place some dummy file, i.e. test.png in that foo directory. Now using asset('foo/test.png'), you should be getting the full and valid URL to test.png file.
If it doesn't, maybe you have configured the asset helper's URL to some other destination using ASSET_URL in .env file.
I think you should not include storage or public , if you have symlink. You can call directly your asset like:
<img src="{{ asset('cover_images/img1.jpg') }}" alt="">

Laravel React-router 404 for Resource files (CSS/JS)

I'm working with Laravel (5.6) and React (16.4.1+react-router) on a project, and got into a problem maybe some of you having similar problem, so I wonder we could get some ideas. Recently I have found a solution for refreshing error (404) here: How to use React Router with Laravel? and one of the soulutions solved /app/{anything} problem, too.
Now my problem is with resource files (/css, /js) from /public folder. I tried to do a workaround with the same regex as /app =>
Route::get( '/{path?}', function() {
return view( 'home' );
})->where('path', '^((?!app|api|css|js).)*$');
but it's still not working as expected. As I read, we must put this snippet after all routes we included in our app, because of Laravel's caching mechanism, but it's not working with neither /css, neither /js resource files in /public folder > but works with /api and /app routes, and anything I add before /{path?}.
Is there any solutions for this? What am I doing wrong?
Thanks for any help,
LeFizzy.
I think I've found a solution for this, and I thought I must share this with others looking for this. First, it was by a mistake. In the home.blade.php view I linked my resource files incorrectly, like this: <link rel="stylesheet" href="http://mydomain.something/css/styles.css" /> and that's why it didn't find the route, and matched with /{path?/ - so it's actually rendered the home view rather than returning the resource file.
For this, Laravel offers a solution for this by looking for your assets in /public folder, just by linking your files simple like: <link rel="stylesheet" type="text/css" href="{{asset('css/styles.css')}}" /> and almost same solution for JS (local) assets: <script src="{{asset('js/app.js')}}" ></script>.
To mention, you should link external (from other networks) like I did with my Laravel app which didn't worked due to the problem I asked for.
Good luck with it. :)

Laravel 5 - Loading Public JS / CSS / HTML Files From Custom Folder?

Laravel serves client side (HTML, CSS, JS) files from the /public folder by default. I am currently transitioning a Blade based front-end to an Angular based one. As a result, I am mixing Blade templating with Angular templating. I am using Blade layouts to generate a navbar, footer, and other common views while I transition the body content of my pages to Angular.
I have a folder constructed just for storing Angular files. My current app structure looks something like this:
-MyApplication
-angular
-app
-bin
-bootstrap
-config
-database
....
Is there anyway for Laravel to load my assets - stored in the /angular folder? I want to keep all my Angular files in one place, in standard Angular structure, as opposed to spreading them out and placing them in the /public Laravel folder.
You can try to create a symbolic link inside the public directory to your intended angular directory. Open your terminal and create a symbolic link like so:
ln -sfv ~/path/to/MyApplication/angular ~/path/to/MyApplication/public/angular
Don't forget to update the ~/path/to/MyApplication to your actual Laravel directory. Now you may refer to the javascript file inside the angular directory like this on your blade template:
<script src="{{ asset('angular/app.js') }}"></script>
Hope this help!

keeping safe assets folder in codeigniter

Hi i have this assets folder which has the following folder inside of css, images, uploads, and js folder i put the assets folder outside application. Im wondering that how could i keep it safe so that when users tried to type in the url like for example http:/test.com/assets it will redirect to application folder or somewhat it will says page not found. Cause ive noticed that when i type in the url http://test.com/assets it will go to the assets folder and which is vulnerable and people could see all the folders in the assets folder. can someone help me figued this thing out? Any help is muchly appreciated.
if you want to secure you folder then do one this create html file like this
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>
and save it as index.html and put it on your assets folder whenever some try to access your url http://test.com/assets then this index file will execute and its shows Directory access is forbidden.

How to link assets(images,stylesheet etc) in Views for CodeIgniter 2.1?

I am using CodeIgniter Version 2.1 & trying to link assets like images,stylesheets, javascript files etc in my views by using, header.php:
<link href="<?php base_url();?>css/style.css" rel="stylesheet" />
my controller code, calls the view:
<?php
class Main extends CI_Controller{
public function index() {
$this->load->view('header');
}
The view file from which I am trying to load the asset is located
../application/views/header.php.
the css file is loaded:
../application/views/css/style.css
this does not work. I get 404 - Page not found error.then, I tried moving css/style.css outside ../application directory in webroot. To my surpise, having assets in webroot(outsite ../application/views) seems to work nicely.
Now,
My Question is
Is having our assets directly in webroot, outsite ../application directory right approach? If YES/NO, then why?
If having assets directly in webroot is a good idea, than should I move ../application/views directory in webroot as well? How?
PS: I am new to CodeIgniter framework, so unaware of the best practices
If you see this, the section where .htaccess is used to remove index.php from the urls, that has a rewrite condition:
RewriteCond $1 !^(index\.php|images|robots\.txt)
In this line, images is a folder that will be ignored while mod-rewriting. So, you can replace it by assets, put all your directly linking files there, and happily use them.
RewriteCond $1 !^(index\.php|assets|robots\.txt)
EDIT: my bad, hadn't seen the date the question was asked. nevertheless, should be useful for someone.
It is best to put your assets in the webroot folder. When requesting assest from the server (depending on your setup), it will start from the root directory and work it's way down.
http://yoursite.com/application/views/css/mystyles.css
if it's in the root you only need to go from there
http://yoursite.com/css/mystyles.css
Although it might be worth putting them all inside a folder (/assets) to keep them contained, as well as the ability to write a more effective rewrite rule (if you are trying to remove the index.php from the url) to ignore the single folder instead of all the individual folders (/css, /js, etc)
As for the views folder, it's best if you leave it in the application folder as CodeIgniter has a built in loader that automatically checks /application/views for the view file when you use the code $this->load->view('myview')
Although there are way to move the views folder, if you are new to CI it's probably best to leave it there for now.
For linking css you can do something like:
echo link_tag('css/mystyles.css');
Check this for ref: http://codeigniter.com/user_guide/helpers/html_helper.html

Resources