CodeIgniter Forums

Full Version: Parse error including vendor/autoload.php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey all,

Have a project in CI 2.2.1 and have installed composer. I'm working on setting up the Mixpanel PHP library (https://github.com/mixpanel/mixpanel-php), and got the composer.json file done, ran my update, and so on.

When I require the vendor/autoload.php, I get this error and the screen goes completely blank:
PHP Parse error:  syntax error, unexpected ';' on line 241

At the bottom of my main index.php file, I've added this line which is causing it (line 241):
require 'vendor/autoload.php';

Which is straight out of a Phil Sturgeon article on composer & CI.

Anyone have any ideas on how I can resolve this? Any help is appreciated!

Thanks.
Sorry, just noticed you said 2.2.1

if this was a 3 version then remove the line of code you added to index.php and then simply set the flag in config.php to TRUE or the path of your vendor folder.

Ive been running 3 for ages and its fine
(03-30-2015, 12:32 AM)futurewebs Wrote: [ -> ]Sorry, just noticed you said 2.2.1

if this was a 3 version then remove the line of code you added to index.php and then simply set the flag in config.php to TRUE or the path of your vendor folder.

Ive been running 3 for ages and its fine

Have messed with the 3 upgrade, haven't quite ironed out all the details.

Thanks though! Hopefully we can get this working in our current version anyway
If you're just putting the call to load the Composer autoloader straight into your index.php file, I would recommend moving the call above the require_once() call to CodeIgniter.php and try something like this:

PHP Code:
           if (file_exists(APPPATH 'vendor/autoload.php')) {
 
               require_once(APPPATH 'vendor/autoload.php');
 
           } elseif (file_exists(APPPATH '../vendor/autoload.php')) {
 
               require_once(APPPATH '../vendor/autoload.php');
 
           

You can take out the conditional which doesn't apply to the location of your autoloader, I simply copied this from what I use within Bonfire (though I load it in a hook so I can use the same config setting which is used in CI3).

If you put it after the call to CodeIgniter.php in your index.php file, it's too late to autoload anything.
Thanks. I actually have it above the CI bootstrap require_once. Still error out for whatever reason. Suppose I may just have to write a library.
Whats the new error? Are you using the APPPATH constant like mwhitney showed? If so then your vendor dir must be located WITHIN your /application dir. What's your file structure like?

If it's like
Code:
/public_html
  /application
  /vendor

Then you'd want to use FCPATH instead of APPPATH, using his example code.
Thanks for the help. Doesn't seem to matter where the /vendor folder is - can put it in APPPATH or in the root and use FCPATH. Returns a 500. Here's the code:
PHP Code:
if (file_exists(APPPATH 'vendor/autoload.php')) {
 
               require_once(APPPATH 'vendor/autoload.php');
 
           } elseif (file_exists(APPPATH '../vendor/autoload.php')) {
 
               require_once(APPPATH '../vendor/autoload.php');
 
           

Path: /application/vendor

Code:
/html
 /application
   /vendor
Quick update on this - I can get the library to work when I load it in an individual controller when loading it like this:

PHP Code:
include APPPATH 'vendor/mixpanel-php/lib/Mixpanel.php';
$mp Mixpanel::getInstance("f8d9db172d290de89a69e2b8414e497a"); 

It's selective about what events it lets me track though. I tried to track the user...
PHP Code:
$user $this->session->userdata('email');
$mp->identify($user); 


But it wouldn't let me. I could, however, track clicks. Very strange. I was starting to write a library for it, but when I load it, same thing: 500 error. As you can see, it's pretty simple...

PHP Code:
class Mixpanel_lib
{
 public function 
__construct()
 {
     include 
APPPATH 'vendor/mixpanel-php/lib/Mixpanel.php'
 
 
               //set token
     
$mp Mixpanel::getInstance("OUR_TOKEN");

 
       }

Just going back and looking at this, then checking against the CI repo, it looks like the default index.php for 2.2.1 is currently ~205 lines, so really line 241 doesn't tell us much about the state of the application when you attempt to load Composer.

If you use APPPATH or FCPATH, you need to make sure they're defined, first. In the default index.php, APPPATH is defined right before the call to CodeIgniter.php, so we're really talking about loading Composer between the definition of APPATH and the require_once for CodeIgniter.php. My index.php is more or less the one included with Bonfire, and it's a bit of a mix between 2.2.1 and 3.0. In Bonfire's case line 241 is before APPPATH is defined.

If you put it into a library, it's going to depend on where you load the library.

Another thing to consider is that an error like "syntax error, unexpected ';'" may indicate some error on a previous line, like an unterminated string (mismatched quotation marks, perhaps?) or an unmatched brace on an if/else, switch, or loop.