skip to Main Content

For example, this is my file structure:

  • MyProject
    • app
    • bootstrap
    • config
    • database
    • node_modules
      • pdfjs-dist
        • build
          • pdf.js
    • public
      • upload
        • documents
          • solution_12.pdf
    • resources

So when i type http://127.0.0.1:8000/uploads/documents/solution_12.pdf (1) into my web browser, it works fine, the web brower shows the pdf file. But when i type http://127.0.0.1:8000/node_modules/pdfjs-dist/build/pdf.js (2), i get not found error.
So can someone explain to my how does path in laravel works? How come the (1) link doesnt contain "public" route, (upload/documents/solution_12.pdf is stored inside public folder) but it still works? And why the (2) link returns not found?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the answer. Can you tell me more about the vite part. I did a little reseach, and this is what i did:

    At vite.config.js, inside defineConfig:

    plugins: [
            laravel({
                input:
                    [
                        'node_modules/pdfjs-dist/build/pdf.js'
                    ],
                refresh: true,
            }),
        ],
    

    Inside app.js, i import the pdf.js script:

    import '../../node_modules/pdfjs-dist/build/pdf.js';
    

    And to use this script, at my resources/views/instructor/course/edit.blade.php I add:

    <script src="node_modules/pdfjs-dist/build/pdf.js"></script>
    

    But the console say that my pdfjsLib cant be found. WHat did i do wrong?


  2. Laravel serves the whole application from public/index.php file, that means when you run php artisan serve or host on the server/cloud, you have to point your domain root to /public. so, the server can access all the files from the public folder. if you wish to access files from the node_modules, you have to add the file paths to your laravel mix or vite configuration and that will copy/compile them into the public folder.

    For the best practices, the files should be uploaded in the storage folder and you can symlink it to the public folder by executing the php artisan storage:link command.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search