PHP Fatal error: Class 'Slim' not found - php

session_start();
date_default_timezone_set('GMT');
require 'Slim/Slim.php';
use Slim\Slim;
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
require_once 'item.php';
this is code excerpt from index.php and stuck on the said error when it called item.php. Here the contains of the file
$app->put('/getItem', authorize(), 'getItem');
function getItem() {
$sql = "SELECT * FROM item";
$app = Slim::getInstance();
try {
$db = getConnection();
$stmt = $db->query($sql);
$item = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
$response = $app->response();
$response->header('Content-Type', 'application/json');
// Include support for JSONP requests
if (!isset($_GET['callback'])) {
echo json_encode($item);
} else {
echo $_GET['callback'] . '(' . json_encode($item) . ');';
}
} catch(PDOException $e) {
$error = array("error"=> array("text"=>$e->getMessage()));
echo json_encode($error);
}
}
i hit the error on this $app = Slim::getInstance();
What is wrong with my approach?

The Slim class' full name (including namespace) is \Slim\Slim so you'll need to use that, eg
$app = \Slim\Slim::getInstance();
Alternatively, you can import the Slim symbol using the use statement at the top of your item.php script.
use Slim\Slim;

You can use this code for Slim Framework3:
<?php
require "vendor/autoload.php";
use \Slim\App;
$app = new App();
$app->get("/",function(){
echo "Hello World";
});
$app->get("/test",function(){
echo "Hello World";
});
$app->run();
?>

Related

class not found if included from extern

I have a this folder structure:
+ inc
++ functions.php
++ vendor
+ private
++ carddav
+++ check.php
my check.php looks like this and works:
<?php
require '../../inc/functions.php';
// CardDAV API
require '../../inc/vendor/autoload.php';
use MStilkerich\CardDavClient\{Account, AddressbookCollection, Config};
use Psr\Log\{AbstractLogger, NullLogger, LogLevel};
use Sabre\VObject\Component\VCard;
class StdoutLogger extends AbstractLogger {
public function log($level, $message, array $context = array()) {
if ($level !== LogLevel::DEBUG) {
$ctx = empty($context) ? "" : json_encode($context);
echo ">>> ".$message . $ctx . "<br />";
}
}
}
Config::init(new StdoutLogger());
$account = new Account(URL, USERNAME, PASSWORD);
$abook = new AddressbookCollection(URL, $account);
$vcard = new VCard();
?>
But now I would like to outsource this part into my functions.php:
functions.php
<?
// CardDAV API
require 'vendor/autoload.php';
use MStilkerich\CardDavClient\{Account, AddressbookCollection, Config};
use Psr\Log\{AbstractLogger, NullLogger, LogLevel};
use Sabre\VObject\Component\VCard;
class StdoutLogger extends AbstractLogger {
public function log($level, $message, array $context = array()) {
if ($level !== LogLevel::DEBUG) {
$ctx = empty($context) ? "" : json_encode($context);
echo ">>> ".$message . $ctx . "<br />";
}
}
}
Config::init(new StdoutLogger());
$account = new Account(URL, USERNAME, PASSWORD);
$abook = new AddressbookCollection(URL, $account);
?>
check.php
<?php
require '../../inc/functions.php';
$vcard = new VCard();
?>
But now I get this error when I open the check.php:
Fatal error: Uncaught Error: Class 'VCard' not found in check.php:26
Where is my mistake?
Tank you !
Imports using use statement work on a per file basis. I.e. classes need to be imported in the file where they're used. In your case remove Sabre\VObject\Component\VCard import from functions.php and move it to check.php where VCard class is actually used.

class not found even it's included PHP

the path also correct but i still don't know why it shows class not found as a fatal error
<?php
ini_set('display_errors', 1);
require_once('../core/Http/Response.php');
use core\Http;
if(class_exists("Response")){
echo "ok";
}
else {
echo "doesn't exist";
}
$r = new Response();
$r->header('test');
?>
normal call in php file,
<?php
ini_set('display_errors', 1);
require_once('../core/Http/Response.php');
use Core\Http\Response;
if(class_exists( 'Core\Http\Response' )){
$r = new Response(); // -OR- $r = new Core\Http\Response();
}else{
die('class doesn\'t exist');
}
$r->header('test');
?>
If you want to call in any controller, call like following
<?php
// Start of the file
ini_set('display_errors', 1);
require_once('../core/Http/Response.php');
use Core\Http\Response;
// your class
class Test extends Test_Controller {
public function test(){
if(class_exists( 'Core\Http\Response' )){
$r = new Response(); // -OR- $r = new Core\Http\Response();
}else{
die('class doesn\'t exist');
}
$r->header('test');
}
}
?>
<?php
ini_set('display_errors', 1);
require_once dirname(__DIR__) . '/Core/Http/Response.php';
use \Core\Http\Response;
$test = new Response("test response");
echo $test ;
?>

Send SMS using vTiger instance

<?php
include_once 'vtlib/Vtiger/Utils.php';
require_once('include/database/PearDatabase.php');
require_once 'config.inc.php';
require_once 'includes/Loader.php';
require_once 'modules/SMSNotifier/SMSNotifier.php';
vimport ('includes.runtime.EntryPoint');
$user = Users_Record_Model::getCurrentUserModel();
GLOBAL $adb;
$tonumbers = array('994255xxxx');
$message = "Test From Ramesh";
$provider = SMSNotifierManager::getActiveProviderInstance();
if( $provider ){
$provider->send($message, $tonumbers);
}
?>
Get provider instance using getActiveProviderInstance. and call send function from that instance.
Try this
<?php
include_once 'includes/main/WebUI.php';
require_once 'includes/Loader.php';
require_once 'modules/SMSNotifier/SMSNotifier.php';
$tonumbers = array('994255xxxx');
$message = "Test From Ramesh";
SMSNotifier::fireSendSMS($message, $tonumbers);

Slim Framework - Getting "500 Internal Server Error" for a simple "$request->get"

I'm fairly new to the Slim framework and I'm getting an error in a very basic call $request->get.
provision/hosts - OK
provision/hosts/28E34748B48E - OK
provision/hosts/search?hostname=ACACA - NOK
Although var_dump($_GET) returns:
array(1) {
["hostname"]=>
string(5) "ACACA"
}
Contents of the index.php file:
<?php
require 'vendor/autoload.php';
//With default settings
$app = new \Slim\App;
$app->get('/hosts', function ($request,$response,$args) {
require_once 'db.php';
$query= "SELECT * FROM hosts";
$result = $mysqli->query($query);
while($row=$result->fetch_assoc()) {
$data[]=$row;
}
if(isset($data)) {
header('Content-Type: application/json');
echo json_encode($data);
}
});
$app->get('/hosts/search', function ($request,$response,$args) {
require_once 'db.php';
//echo var_dump($_GET);
$hostname=$request->get('hostname');
echo $hostname;
});
$app->get('/hosts/{macaddr}', function ($request,$response,$args) {
require_once 'db.php';
$query= "SELECT * FROM hosts WHERE macaddr='".$args['macaddr']."'";
$result = $mysqli->query($query);
$data=$result->fetch_assoc();
if(isset($data)) {
header('Content-Type: application/json');
echo json_encode($data);
}
});
$app->run();
?>
The method get doesn't exist in Slim\Http\Request
Fatal error: Call to undefined method Slim\Http\Request::get() in /slim3/index.php on line
You need to use getParam
$hostname = $request->getParam('hostname');

How to do a catch all in Slim framework?

I'm making a short URL service using Slimphp to take care of my routing. I can define any route just fine but if I want to react to /<code here> instead of that taking me to the index page of the project.
This is my code:
<?php
require 'vendor/autoload.php';
use ShortUrls\ShortUrls;
error_reporting(E_ALL);
ini_set('display_errors', true);
$app = new \Slim\Slim();(array(
"view" => new \Slim\Views\Smarty()
));
$view = $app->view();
$view->parserDirectory = dirname(__FILE__) . 'vendor/smarty/smarty/libs';
$view->parserCompileDirectory = dirname(__FILE__) . '/compiled';
$view->parserCacheDirectory = dirname(__FILE__) . '/cache';
$view->setTemplatesDirectory(dirname(__FILE__) . '/lib/templates/');
\ShortUrls\Config::init_config();
$app->get('/', function ($hash) {
try {
} catch (ResourceNotFoundException $e) {
echo '404';
}
$short = new ShortUrls();
if ($hash) {
if ($short_url = $short->get_url_by_hash(($hash))) {
print '<pre>';
print_r($short_url);
print '</prE>';
}
} else {
$short->create_short_url("http://www.locovsworld.com");
// $app->render('layout.tpl', array('test' => 'Hello'));
}
global $app;
print_r( $app->request()->params() );
echo 'done';
});
$app->run();
Remember / == index /9082ABC could be a short URL that I have to query from the database and redirect the client to.
I already got the answer its the following ...
$app->get('/(:hash)', function ($hash) {
};
I am sorry to bother you guys :(

Categories