php Fatal error: Uncaught Error: Class 'Router' not found in - php

<?php
$query = require 'core/bootstrap.php';
$router = new Router();
require 'routes.php';
$uri = trim($_SERVER['REQUEST_URI'],'/');
require $router->direct($uri);
I don't get what I missed, tried to require Router.php file but nothing help searched for google for that error bug was getting only cake.php answers
$router ->define([
' ' =>'controllers/index.php',
'about' => 'controllers/about.php',
'about/culture' => 'controllers/about-culture.php',
'contact' => 'controllers/contact.php'
]);
that's routes.php file

Hey You are requiring routes.php after new Router(); line. I assume your Router class is present in routes.php file
Hence try below code
$query = require 'core/bootstrap.php';
require 'routes.php';
$router = new Router();
$uri = trim($_SERVER['REQUEST_URI'],'/');
require $router->direct($uri);

Related

How to import/use package installed with Composer?

I'm just getting started with PHP and I ran into a small problem.
I've downloaded a package using composer require <package_name> and now I'm not sure how to access it from my .php file. I tried bunch of things but I couldn't make it work.
I'm trying to use this package: https://packagist.org/packages/giggsey/libphonenumber-for-php
EDIT:
This is the code I use to test:
<?php
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
// Instantiate app
$app = AppFactory::create();
// Add Error Handling Middleware
$app->addErrorMiddleware(true, false, false);
// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
$swissNumberStr = "044 668 18 00";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {
$swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH");
var_dump($swissNumberProto);
} catch (\libphonenumber\NumberParseException $e) {
var_dump($e);
}
// Run application
$app->run();
And it says: Undefined type 'libphonenumber\PhoneNumberUtil'.

How to install Phalcon php

I downloaded Phalcon from official website
I copied php_phalcon.dll file to my xampp's php/ext directory
Edited the php.ini file located at D:\xampp\php\php.ini. and add there line extension=php_phalcon.dll at the end of the file.
I Restarted apache server and computer several times.
When I write phpinfo() to my code it seems phalcon was installed
Unfortunatelly Whan I try to run some code like
<?php
try {
// Autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs([
'../app/controllers/',
'../app/models/'
]);
$loader->register();
// Dependency Injection
$di = new \Phalcon\DI\FactoryDefault();
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views');
return $view;
});
// Deploy the App
$app = new \Phalcon\Mvc\Application($di);
echo $app->handle()->getContent();
} catch(\Phalcon\Exception $e) {
echo $e->getMessage();
}
?>
I get this error
Fatal error: Uncaught Error: Class 'Phalcon\Loader' not found in D:\xampp\htdocs\php-learning\public\index.php:4 Stack trace: #0 {main} thrown in D:\xampp\htdocs\php-learning\public\index.php on line 4
I also tried to follow the steps from tutorial on Phalcon ofical website where code looks somehow like this
<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Url;
// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
// Register an autoloader
$loader = new Loader();
$loader->registerDirs(
[
APP_PATH . '/controllers/',
APP_PATH . '/models/',
]
);
$loader->register();
$container = new FactoryDefault();
$container->set(
'view',
function () {
$view = new View();
$view->setViewsDir(APP_PATH . '/views/');
return $view;
}
);
$container->set(
'url',
function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
}
);
$application = new Application($container);
try {
// Handle the request
$response = $application->handle(
$_SERVER["REQUEST_URI"]
);
$response->send();
} catch (\Exception $e) {
echo 'Exception: ', $e->getMessage();
}
But didn´t help. What I am doing wrong?
You have Phalcon installed successfully. However the namespace should be changed:
Moved Phalcon\Loader to Phalcon\Autoload\Loader #15797
please refer to the change log
enter link description here

How to fix "Class not found" Error for the Google Sheets API?

I am trying to set up a website that has google sheets API functionality, due to the constraints of the hosting service, I installed the API on my local computer with Composer before uploading everything onto the site. However, when I try creating the Google_Service_Sheets object, it says that the class cannot be found.
I've tried recreating the autoload.php file, and also adding a seperate require function:
require_once "./googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets.php";
Main code:
require_once $configs["googleapifilev2"];
require_once "./googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets.php";
$client = new \Google_Client();
$client->setApplicationName('test');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig($configs["googlecredentials"]);
$service = new \Google_Service_Sheets($client);
autoload.php code:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit68a7a3a3b2a74c49476ad55dd7b1c990::getLoader();
An error occurs only when I call the Google_Service_Sheets object, but not the Google_Client.
Error message:
Fatal error: Uncaught Error: Class 'Google_Service_Sheets_Resource_Spreadsheets' not found in /storage/ssd3/963/10211963/public_html/googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets.php:69
Stack trace:
#0 /storage/ssd3/963/10211963/public_html/twowvotingaction.php(56): Google_Service_Sheets->__construct(Object(Google_Client))
#1 {main} thrown in /storage/ssd3/963/10211963/public_html/googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets.php on line 69
(Related lines in the API sheets.php file):
public function __construct(Google_Client $client, $rootUrl = null)
{
parent::__construct($client);
$this->rootUrl = $rootUrl ?: 'https://sheets.googleapis.com/';
$this->servicePath = '';
$this->batchPath = 'batch';
$this->version = 'v4';
$this->serviceName = 'sheets';
$this->spreadsheets = new Google_Service_Sheets_Resource_Spreadsheets( //Line 69
$this,
$this->serviceName,
'spreadsheets',
For some reason, when uploading the files, I accidentally put the ./googleapi/google-api-php-client-2.2.3/vendor/google/apiclient-services/src/Google/Service/Sheets folder, which defines the class, in the wrong directory, I moved it, and it now works.

Google Cloud Storage 500 Internal Server Error

SettingsController.php:
namespace App\Controllers;
use Google\Cloud\Storage\StorageClient;
class SettingsController extends Controller {
public function createBucket($request, $response) {
$projectId = 'myProjectId'; //here I specified my projectId
$storage = new StorageClient([
'projectId' => $projectId
]);
$bucketName = 'socnetfilestestkekdsfa213kfh34';
$bucket = $storage->createBucket($bucketName);
}
}
So, I wrote this code in accordance with documentation, but I'm always getting 500 Network Error.
This is my index.php:
<?php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../config/db.php';
$app = new \Slim\App(['settings' => $config]);
require __DIR__ . '/../app/dependencies.php';
require __DIR__ . '/../app/routes.php';
$app->run();
And this is my project structure:
Finally, I've solved the problem.
As I mentioned require __DIR__ . '/../vendor/autoload.php'; was correct. In my case, the reason of 500 Error was an environment variable. For the deployed app you should put your Service Google Cloud Account key config.json into your project. I put mine in a config folder (check my project structure above).
It means that in Heroku 'Settings' i need to specify
GOOGLE_APPLICATION_CREDENTIALS: ../config/yourAppName.json
Thanks this article:
https://medium.com/#naz_islam/how-to-authenticate-google-cloud-services-on-heroku-for-node-js-app-dda9f4eda798

Error using Slim framework and Twig template

I'm trying to make Slim work with Twig template system, this is part of my index.php
// Twig [Template]
require 'Extras/Views/Twig.php';
TwigView::$twigDirectory = __DIR__ . '/vendor/Twig/lib/Twig/';
//Slim
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim(array(
'view' => $twigView
));
And this is my structure
Extras
|_Views
|_Twig.php
Slim
templates
vendor
|_Twig
|_lib
|_Twig
index.php
I try several times with other configurations and searching buy I ALLWAYS get this error:
Fatal error: Class 'Slim\View' not found in C:\wamp\www\slim\Extras\Views\Twig.php on line 43
Can anyone help me here? All the examples I had found was using composer
Ok, I solve it.
This is the solution:
// Slim PHP
require "Slim/Slim.php";
\Slim\Slim::registerAutoloader();
// Twig
require "Twig/lib/Twig/Autoloader.php";
Twig_Autoloader::register();
// Start Slim.
/** #var $app Slim */
$app = new \Slim\Slim(array(
"view" => new \Slim\Extras\Views\Twig()
));
And this is my structure now.
Slim
|_Extras
|_Views
|_Twig.php
|_Slim
templates
Twig
|_lib
|_Twig
|_Autoloader.php
index.php
¡I hope this help someone else!
Now Slim-Extras is DEPRECATED, we must use Slim-Views (https://github.com/codeguy/Slim-Views):
require "Slim/Slim.php";
\Slim\Slim::registerAutoloader();
$slim = new \Slim\Slim( array(
'debug' => false,
'templates.path' => 'fooDirTemplates',
'view' => '\Slim\Views\Twig'
));
$twigView = $slim->view();
$twigView->parserOptions = array(
'debug' => false
);
$twigView->parserDirectory = 'Twig';
$twigView->parserExtensions = array(
'\Slim\Views\TwigExtension'
);
$slim->notFound( 'fooNotFoundFunction' );
$slim->error( 'fooErrorFunction' );
// SLIM routes...
$slim->run();
If someone is still running in to this issue.
The problem for me was that I had installed both slim/views AND slim/twig-view.
I uninstalled slim/views and it worked

Categories