Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get to the public directory

I use angular-cli in my project, and I want to use a global image - my website logo image.

As I understand the public directory at the root of the project (which angular-cli created) is for public assets - which seems like a good place to put my image (please correct me if I'm wrong).

But, when I try to get to that image in my <img src="/public/book4u-logo.svg" /> tag, I get 404. The reason is, of course, my project root is the src directory and not the project root directory.

So, how should I get to that directory? Or should I place my image in another directory?

My project tree (i remove the unnecessary suff):

.
├── angular-cli-build.js
├── angular-cli.json
├── package.json
├── public
│   ├── book4u-logo.svg
│   └── hero-image-default.jpg
├── src
│   ├── app
│   │   ├── bfy.component.html
│   │   ├── bfy.component.scss
│   │   ├── bfy.component.ts
│   │   ├── environment.ts
│   ├── favicon.ico
│   ├── index.html
│   ├── main.ts
│   ├── system-config.ts
│   ├── tsconfig.json
│   └── typings.d.ts
├── tslint.json
└── typings.json

The img tag is at /src/app/bfy.component.html and the image itself is at /public/book4u-logo.svg.

like image 758
Nirgn Avatar asked Jun 19 '16 20:06

Nirgn


People also ask

What is a public directory?

The Public directory (/home/group/public) is used to share files with members in other groups. Users have read access to public files belonging to other groups, but they may not write or add to files of another group. Any user may write and add files to their group's Public directory.

Where is public path in Laravel?

The best way to retrieve your public folder path from your Laravel config is the function: $myPublicFolder = public_path(); $savePath = $mypublicPath. "enter_path_to_save"; $path = $savePath.

What is public folder in angular?

The public folder is for public assets and the content is copied to the /dist folder. However, when running ng build --prod the javascript in the /public folder is not copied over to /dist .

What is Laravel root directory?

The root directory of a fresh Laravel installation contains a variety of directories: The app directory, as you might expect, contains the core code of your application. We'll explore this directory in more detail soon.


3 Answers

The Angular-cli places all files in the /dist directory.

To display the image in index.html: <img src="book4u-logo.svg" />

If you want to use your image in your app you should use a folder in the /src directory like src/shared/assets/img/book4u-logo.svg

The public folder is for public assets and the content is copied to the /dist folder. However, when running ng build --prod the javascript in the/public folder is not copied over to /dist.

There is an open issue on Github:Static JS assets not copying The image placed in the public folder should be accessible from the dist folder.

like image 177
KB_ Avatar answered Oct 28 '22 04:10

KB_


Latest version of angular-cli(angular-cli: 1.0.0-beta.19-3) provides an assets folder under src directory. You just need to add src property of an image tag as below, <img src="./assets/logo.png">

Note: When you build the project using ng serve it will copy your assets folder to dist folder.

like image 34
Suchin Avatar answered Oct 28 '22 03:10

Suchin


I solved a similar problem in Angular 4 (I had a css file that was in public folder and I put it in assets folder). The answer would be:

  • file path: ./src/assets/book4u-logo.svg
  • <img src="./assets/book4u-logo.svg>
like image 38
Joey Avatar answered Oct 28 '22 03:10

Joey