Welcome Guest, Not a member yet? Register   Sign In
Parse error including vendor/autoload.php
#1

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.
Reply
#2

(This post was last modified: 03-30-2015, 12:34 AM by futurewebs.)

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
Reply
#3

(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
Reply
#4

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.
Reply
#5

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.
Reply
#6

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.
Reply
#7

(This post was last modified: 04-06-2015, 11:47 AM by cleetus.)

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
Reply
#8

(This post was last modified: 04-06-2015, 12:29 PM by cleetus.)

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");

 
       }

Reply
#9

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB