I am writing a helper method to read and convert a xml data into json. I followed the steps below
1) Created file
app/helper/commonHelper.php
and added following code to it.
<?php
function xmlToArray($xml, $options = array()) {
// the entire code here
}
2) Created a file under
app/providers/HelperCommonsProvider.php
and following code
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperCommonsProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
require base_path().'/app/Helpers/CommonHelper.php';
}
}
Now when I call xmlToArray() in the controller like
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Config;
use Log as EventLogger;
class UsersController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index() {
echo "You are in the index function";
$file = Config::get('constants.constants.userdirectory');
$file = $file . '/' . 71 . '/' . 71 . '.xml' ;
$xmlloaded = simplexml_load_file($file);
$returnvalues = xmlToArray($xmlloaded, array('alwaysArray'));
echo json_encode($returnvalues);
die;
//
}
}
This throws an exception
Fatal error: Cannot redeclare xmlToArray() (previously declared in D:\\work\\HC\\hcserver\\app\\Helpers\\CommonHelper.php:3) in
D:\\work\\HC\\hcserver\\app\\Helpers\\CommonHelper.php on line 79 PHP Fatal error: Uncaught exception 'Illuminate\\Contracts\\Container\\BindingResolutionException' with message 'Target [Illuminate\\Contracts\\Debug\\ExceptionHandler] is not instantiable.' in D:\\work\\HC\\hcserver\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php:748
Please tell me what am I doing wrong here?
UPDATED: Entire content inside CommonHelper.php -> http://pastebin.com/GuQGYnJP
you should change name from
xmlToArray to xmlToArrayCustom
Reason: its global function which make sense to laravel and php. because this method name is used by PHP Library and cannot be redaclared with the same name.
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.