简体   繁体   中英

Including .php files with Laravel

I have to use jcart Carting with a project that I just started building on with Laravel.

With usual php projects, the steps involved is just dumping the 'jcart' folder in the root, and referring the paths to the jcart.php, and jcart.js files.

I just started with laravel, and it's getting really confusing for me figure out how to work with this.

In the front end file (using blade I understand), I need to include

/jcart/jcart.php

And in jcart.js file, I need to refer again to /jcart/jcart.php (and other files).

How ever, across the site, it keeps throwing not found errors in the javascript file.

I have dumped the jcart folder in /public/. Is there a better way to do this?

Thanks in Advance

The way I do includes is as follows. Here is the folder structure of your Laravel project.

app
-> commands
-> config
...
-> includes
  -> jcart
    -> jcart.php
    -> config.php
    -> config-loader.php
    -> relay.php

By default, there is no includes folder, but it is perfectly fine to modify the app folder to suit your needs. If you follow this structure, you can then include a file using the following:

require app_path()."/includes/jcart/jcart.php";
require app_path()."/includes/jcart/config.php";
require app_path()."/includes/jcart/config-loader.php";
require app_path()."/includes/jcart/relay.php";

Hope that gives some insight!

Edit

Change the folder structure to:

public
-> assets
  -> js
    -> jcart
      -> php
        -> jcart.php
        -> config.php
        -> config-loader.php
        -> relay.php
      -> js
        -> jcart.js
        ...

Than you would change the path variable in jcart.js to:

var path = 'php',
// OR
var path = '../php',

And hopefully one of those should work.

I'm not sure exactly how jCart integrates, but as far as the JavaScript only the files in /public/ are exposed to the web. If you had a file like /public/foo/bar.js you would refer to it using http://example.com/foo/bar.js .

Laravel has some helpers to automatically generate the correct URL. eg

$url = asset('img/photo.jpg'); 

At times Laravel can appear complex, but it really has some fantastic features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM