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 ;
?>
Related
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.
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');
for a project I am currently working on I am using rabbitMQ to queue tasks, and send messages. If I run the server and the client from a terminal I will receive data, but if I try to include the php in an index.php that tries to display the into to a web page it does not work. What am I doing wrong, I tried wrapping the testRabbitMQClient.php in a function but this still does not work.What am I missing?
<?php
//index.php
require 'openid.php';
require 'functions.php';
require 'testRabbitMQClient.php';
/*
#connects to my database and checks to make sure its connected.
*/
$apikey="key";
try {
$openid = new LightOpenID('localhost');
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'http://steamcommunity.com/openid';
header('Location: ' . $openid->authUrl());
}
?>
<h1>Hey ____ thank you for using FOF.com</h1>
<br>
<?php
//showFriends(xxxxxxxxxxxxxxxxx);
//calls function from testRabbitMQClient.php
send("friends");
?>
<form action="?login" method="post">
<input type="image" src="http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_large_border.png">
</form>
<?php
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
if($openid->validate()) {
$id = $openid->identity;
// identity is something like: http://steamcommunity.com/openid/id/76561197994761333
// we only care about the unique account ID at the end of the URL.
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
echo "User is logged in (steamID: $matches[1])\n";
}
else
{
echo "User is not logged in.\n";
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
?>
#!/usr/bin/php
<?php
//rabbit mq client;
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
function send($type){
$client = new rabbitMQClient("testRabbitMQ.ini","testServer");
if (isset($argv[1]))
{
$msg = $argv[1];
}
else
{
$msg = "test message";
}
$request = array();
$request['type'] = "friends";
$request['username'] = "steve";
$request['password'] = "password";
$request['message'] = $msg;
$response = $client->send_request($request);
//$response = $client->publish($request);
echo "client received response: ".PHP_EOL;
echo $response;
echo "\n\n";
echo $argv[0]." END".PHP_EOL;
}
?>
I figured out my issue! I forgot to enable amqp in apache. I did this by adding the extension=amqp.so to the php.ini file that is located in the apache folder.
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 :(
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();
?>