I have a parse error using eloquent with slim3 - php

I'm following the codecourse video series Authentication with Slim 3, which is about 3 years old.
I've hit a snag when it comes to using eloquent to access a mysql database.
(the relevant video: https://www.youtube.com/watch?v=70IkLMkPyPs )
the code wasn't quite the same as that currently recommended on the instructions on illuminate/database's docs, so I've modified the code slightly to follow that standard, but regardless of whether I follow Alex's code oon the vid or the following, a parse error results.
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true,
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'opium3',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]
],
]);
$container = $app->getContainer();
/*set up eloquent to use outside laravel*/
use \Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
Running the app produced this error:
Parse error: syntax error, unexpected '=' in /var/www/public/opium3/vendor/illuminate/database/DatabaseManager.php on line 65
I'm running this locally on a scotchbox vagrant set up, if that's relevant.
There's a similar question at
Parse error when run illuminate/database/capsule with bootEloquent() in PHP
but this answer doesn't resolve my issue, since the server is running php7.
I have also tried installing packages recommended by illuminate/database, installing php-mbstring, and editing the php.ini file, on the server, but the error persists.
Am I missing something obvious?

Make sure you're using at least PHP 7.1.
/illuminate/database/DatabaseManager.php is using a short hand version of the list()-construct which wasn't available until PHP 7.1.
The "old" way is to:
list($var1, $var2) = someFunction();
while you, since PHP 7.1, can also do:
[$var1, $var2] = someFunction();
The row the error is thrown in is this:
[$database, $type] = $this->parseConnectionName($name);
Note:
If you can't upgrade your PHP version from 7.0 to 7.1.3+, you need to use an older version of the Illuminate/Database-package. Use version 5.5 for PHP 7.0 and then 5.6+ on PHP 7.1.3+

Related

CodeIgniter 3.1.9: Call to undefined function odbc_connect() in MacOS

UPDATE: This question has been flagged as a question that has been solved already. But when checked, those previous "similar" questions require users to download the corresponding .dll file which is not applicable for macOS users and other answers did not explicitly elaborate on how to install needed extensions like pdo_sqlsrv, sqlsrv, and ODBC for XAMPP-installed PHP.
I'm trying to connect to MS SQL database using CodeIgniter 3.1.9 via ODBC:
$db['default'] = array(
'dsn' => '',
'hostname' => 'x.x.x.x',
'username' => 'sa',
'password' => 'xxx',
'database' => 'xxx',
'dbdriver' => 'odbc'
But I'm getting this error:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined function odbc_connect()
Filename: /Applications/XAMPP/xamppfiles/htdocs/gpweb/system/database/drivers/odbc/odbc_driver.php
Line Number: 141
PHP is installed via XAMPP. I checked the php.ini file to get information about ODBC and the line below is disabled because of the semi-colon in the beginning:
;extension=php_pdo_odbc.dll
I know it is irrelevant because I'm running on MacOS and .dll files are for Windows (but I still tried enabling it and with more errors):
A PHP Error was encountered
Severity: Core Warning
Message: PHP Startup: Unable to load dynamic library '/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20160303/php_pdo_odbc.dll' - dlopen(/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20160303/php_pdo_odbc.dll, 0x0009): tried: '/Applications/XAMPP/xamppfiles/lib/php_pdo_odbc.dll' (no such file), '/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20160303/php_pdo_odbc.dll' (no such file)
Filename: Unknown
Line Number: 0
Has anyone encountered this issue before and how did you solve it? How can you enable ODBC in MacOS with PHP installed using XAMPP?
I found the solution, missing in php.ini
extension=php_odbc.dll
Unfortunately, macOS users that use XAMPP to install PHP have no way to install extensions like sqlsrv, pdo_sqlsrv, and ODBC, unlike in Windows, where they can download the necessary .dll files and enable extensions in the php.ini.
I've been using CodeIgniter 3.x.x for years to create web applications and MySQL as the database. I wanted to utilize the said PHP library with my other projects requiring MSSQL as the database since CI can do so, provided that you have installed the necessary extensions.
I have scoured the internet and there is really no definite solution to install extensions in XAMPP-installed PHP running macOS.
I gave up and installed PHP using homebrew instead. In your terminal, enter:
$ brew install php
Then installed sqlsrv extension (for M1 ARM64 users)
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install sqlsrv
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install pdo_sqlsrv
Then the ODBC extension:
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y
brew install msodbcsql17 mssql-tools
Since I cannot use the XAMPP-installed PHP, I have to use the CodeIgniter 4 to use the natively installed PHP:
composer create-project codeigniter4/appstarter project-root
After that, in the /project/app/Config/Database.php of my CodeIgniter 4 project, setup the connection to the database:
public $default = [
'DSN' => '',
'hostname' => 'x.x.x.x', // IP ADDRESS OF THE SQL SERVER
'username' => 'xxx', // USERNAME
'password' => 'xxx', // PASSWORD
'database' => 'xxx', // NAME OF DATABASE
'DBDriver' => 'SQLSRV', // DATABASE DRIVER TO BE USED
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 1433, // DEFAULT PORT FOR SQL SERVER
];
This is not the solution to the original problem (XAMPP-installed PHP), but an alternative solution to connect to MSSQL database using PHP.
REFERENCE:
https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver16
https://www.codeigniter.com/user_guide/installation/index.html
But still, if you found a way to install sqlsrv and ODBC extensions in XAMPP-installed PHP running macOS, please don't hesitate to post it here.

Undefined index: REMOTE_ADDR while Laravel migrate

When I run php artisan migrate on my local server (running by php artisan serve) I get the following error:
[ErrorExeption]
Undefined index: REMOTE_ADDR
I tried also php artisan migrate --database db_name, output is like this:
[InvalidArgumentExeption]
Database [db_name] not configured.
My app/config/local/database.php looks like following:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost', // also tried 127.0.0.1
'database' => 'db_name',
'username' => 'root',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
)
I'm sure MySQL works, I checked it via command line and phpMyAdmin.
Also I included HostnameLookups On inside my httpd.conf, then restarted apache, but nothing helps.
I use Laravel 4.2, Debian.
Any ideas? Thanks in advance.
P.S. Sorry for my English:)
UPD:
Output of php artisan env is Current application environment: local
UPD2:
I created simple route:
Route::get('/test', function() {
return $_SERVER["REMOTE_ADDR"];
});
It returns ::1, but when I execute echo $_SERVER["REMOTE_ADDR"]; in php interactive mode (php -a) I get a Notice: Undefined index: REMOTE_ADDR
When you get an artisan error, that error has possibly nothing to do with the command currently tried to run with artisan, but with some any other coding error you recently made. Search in your code for any recent reference to REMOTE_ADDR.
If you haven't done so, make sure you're accessing SERVER_ADDR using the $_SERVER array to obtain the value of SERVER_ADDR, which is an element of this array.
$_SERVER['SERVER_ADDR'];
If that doesn't work, it may mean that your server doesn't provide that information.
From PHP.net:
There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

Integrating Sentinel with a script

I'm trying to use Cartalyst Sentinel for my scripts. I've downloaded the files from github. (https://github.com/cartalyst/sentinel). I've put it on my wamp, and I am trying to follow the instructions here (https://cartalyst.com/manual/sentinel/2.0#native).
This is what I have so far:
<?php
// Import the Sentinel Classes
use sentinel\src\Native\Facades\Sentinel;
use Illuminate\Database\Capsule\Manager as Capsule;
require 'vendor/autoload.php';
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'sentinel',
'username' => '',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]);
$capsule->bootEloquent();
?> code here
I've followed the instructions but I have changed the directory from the first use... I cannot seem to find vendor/autoload.php. I get the cannot find stream error.
and when I remove that, I get this error.
( ! ) Fatal error: Class 'Illuminate\Database\Capsule\Manager' not found in C:\wamp\www\lab\login.php on line 10
So I'm kind of confused since the manual isn't really providing ways to solve problems and what we need.
Any help would be greatly appreciated!
It seems you have not updated later with the implementation for the Illuminate Database component.
To use it run:
composer require illuminate/database illuminate/events symfony/http-foundation
This will install Illuminate\Database\Capsule hope this works.
I am 4 years late to answer this but believe that this might help someone struggling.

ZF2+Doctrine2 - Suddenly getting error on Mac: "Doctrine\ORM\EntityManager"; no instance returned

I am working on building a Zend Framework 2 (ZF2) website using Doctrine2 DBAL/ORM, where I develop equally on Windows and Mac, sometimes Linux (Ubuntu) (I like the all-around experience).
In composer.json I have the following versions:
"doctrine/doctrine-orm-module": "^0.9.1"
"zendframework/zendframework": ">=2.3.2,<3.0.0"
At first, my website ran fine on all three environments using XAMPP (Apache 2.4.16, MySQL 5.0.11, and PHP 5.6.12). But suddenly, after I performed some changes on Windows and pulled those changes from GitHub to my Mac computer, Doctrine started failing with the following exception message:
An exception was raised while creating "Doctrine\ORM\EntityManager"; no instance returned
I got this exception a lot while configuring Doctrine2 in ZF2. But once I was done, things just worked. That is until it broke on Mac, only! It still works fine on Windows.
I have cleared the cache completely (rm data/cache/*) and I have verified the integrity of my configuration files. The only difference in configuration between Windows and Mac is that I provide a unix_socketpath for MySQL (see below).
My config/application.php:
<?php
return array(
'modules' => array(
// ...
'DoctrineModule',
'DoctrineORMModule',
),
'module_listener_options' => array(
// ...
),
);
My config/autoload/databases.local.php looks like this (with changed values for database server login information):
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock', // This is for Mac
'host' => 'some_host',
'port' => '3306',
'user' => 'some_user',
'password' => 'some_password',
'dbname' => 'some_database',
)
)
),
'configuration' => array(
'orm_default' => array(
'proxy_dir' => 'core/server/data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\Proxy',
)
)
),
);
As mentioned, these configurations work on Windows (I can retrieve and use the EntityManager) and is a 1:1 mirror onto my Mac solution. So what happens?
When I check the cached configuration file, data/cache/module-config-cache.application.config.cache.php, on my Mac, the database login informations are incorrect, having the following values:
// ...
array (
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
),
// ...
Obviously these are wrong. But how can ZF2 suddenly generate incorrect configurations on Mac?
I even tried checkout out previous commits from Git on my Mac, which I know worked for certain. But the same problem occurs. The only difference, I suspect might have influence on this behavior, is that I ran composer.phar update, which might've changed the versions of ZF2 and/or Doctrine2.
The problem boiled down to the glob pattern for the autoload config files being incorrect.
$appConfig['module_listener_options']['config_glob_paths'][$index] = getcwd() . '/' . $path;
getcwd() had an incorrect pointer, despite having specified the following:
define('ROOT_PATH', realpath(__DIR__ . '/../../../..'));
chdir(ROOT_PATH);
For some reason I cannot fathom, it worked before on Windows and Ubuntu, but not on Mac. Now it works in all three environments.
It's an almost invisible error and required line-by-line debugging through the bootstrapping logic.
In my case, this error message were thrown, when the database was missing!

Class not found error when moving to host?

I am trying to make a new Facebook app and got hosting from Asmallorange. The app works perfectly in my local environment that is running PHP 5.5.14.
The app consists of packages that were imported by Composer and autoloaded in my app.
The app in itself is a Slim app and consists of Laravel's Eloquent ORM. I followed tutorials online to integrate those two, and it works perfectly in my local environment.
The code is as follows.
require 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection(array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'test',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
));
$capsule->bootEloquent();
It works perfectly in my local environment. Just not on the server and fails with this:
PHP Fatal error: Class 'Illuminate\Database\Capsule\Manager' not found in /home/moz/public_html/app/index.php
Referring to line 2 above. I have looked everywhere and couldn't find a solution yet.
Check that your deployment tool has uploaded the autoload_classmap.php file found in the vendors/composer directory. Uploading this should fix the problem.
Alternatively you could get composer to rebuild the autoload_classmap.php file on the server by running the following in the command line from same directory as composer...
php composer dump-autoload

Categories