Include statement not working in PHP/Yii2 - php

I have two files, the first one being in the main directory, console_output.php:
<?php
class console_output
{
function write_to_logfile($text) {
file_put_contents("log.txt", $text);
}
}
?>
and the second one in a subdirectory, xController.php
<?php
...import stuff...
include ("../console_output.php");
class xController extends Controller{
...do_stuff..
function doMoreStuff(){
...
$console_output = new console_output();
$console_output->write_to_logfile("did Stuff");
}
}
?>
This is all in Yii2 framework, in case that matters. I have tried with apps/console_output.php and ../console_output.php, but it fails either way.
When I use apps/console_output.php, the error is
include(app/console_output.php) [https://secure.php.net/manual/en/function.include.php'>function.include.php]: failed to open stream: No such file or directory
The error is shown right at the include statement.
Using ../console_output.php in the include statement gives me an error at
$console_output->write_to_logfile("did Stuff");
with the message
Class 'app\controllers\console_output' not found
I have no idea what I'm doing wrong here. Can you help me out please?

Yii(2) offers multiple shortcuts to various paths. The most commonly used one is
Yii::$app->urlManager->baseUrl
So You can include your php file like:
$url = Yii::$app->urlManager->baseUrl.'/../console_output.php';
include $url;

You relative path Def nation is wrong. You are using .. that mean you moving a director up from current script. You need to use "subdirectory/script.php"

Related

PHP - Get class by included url

I want to getting class by included url between two hosts , I had to called class , there is a point which i have to creating an API , like a library to calling whatever. or it must be do by another way ?!
call.php
<?php
class call{
public function Call_Name($c = "") {
$c = "Called";
return $c;
}
}
?>
index.php
<?php
include 'http://example.com/new/call.php';
$call = new call();
echo $call->Call_Name();
?>
(allow_url_fopen , allow_url_include) is already on.
What you are looking for just isn't safe.
Instead of attempting to execute remote code, consider using a package manager such as Composer/Packagist. You can write your general classes once as a "library" and share them among your "packages".
Take a look here: https://getcomposer.org/ for how to get started.

can't bind href with php function

I'm working on a new website. This website will be a one pager. All my files I already load in through PHP into the main folder. But now I want to edit them and update them through WYSIWYG.
The UPDATE and SELECT are already working. I tested it on a page who stood on its own. All the one-pager files are stored in a folder and within the folder is also the file which loads all the files into it. I call them through a href which ends up giving the file an # in the address bar. There lies the problem. I can't access the #file with the function I wrote because that only can access files without starting a #. Is it possible to access it through my function?
I give the code if the question is too unclear because it's a bit of mess to implement all the files I use for this purpose.
Short recap: Can't access #domain with a PHP function. Is it even possible to access it.
<?php
class Home extends Controller {
protected function frontpage() {
$viewmodel = new HomeModel();
$this->returnView($viewmodel->frontpage(), true);
}
}
?>
this is the returnView from Controller:
protected function returnView($viewmodel, $fullview){
$view = 'views/'. get_class($this). '/' . $this->action. '.php';
if($fullview){
require('views/main.php');
} else {
require($view);
}
}
I'm new with overflow so I couldn't get the function into right place but with protected function frontpage I should access the file frontpage.php. Well it does do that but that's not the right directory because it's only visible throug a href which means the function should have been: protected function #frontpage which isn't possible.
This is actually not possible, as the anchor (#) isn't sent to the server and is handled by the browser itself.
The only possibility is to include some JavaScript magic to your project.

Path issue with autoload function to get files for use in Ajax

So my project uses an MVC framework and I have a page with an Ajax script I run to get content from the server. When the PHP script is called in the Ajax script, I want to access the classes already in my library for use in the PHP script. To do this, I use what I call an ajaxBootstrap to call the appropriate function that then instantiates the objects needed for that specific Ajax script.
To load those classes from my library I have an autoload function in my ajaxBootstrap so I don't need to use a bunch of require and include statements. My problem is those files aren't being loaded due to a path issue with the autoload function. When I use a require statement with the same path, the classes load with no problems, its only when I try to load them using the autoload function that I get an 500 internal server error.
Here is my ajaxBootstrap file:
// This file routes Ajax requests made in JS files and instantiates a specific object to carry out the actions needed for that particular Ajax operation
// Autoload any classes that are required
function autoLoad($classToLoad)
{
if(file_exists('../library/' . $classToLoad . 'class.php')) // File in the library folder
{
require('../library/' . $classToLoad . '.class.php');
}
else if(file_exists('../../app/models/' . $classToLoad . 'class.php')) // File in the models folder
{
require('../../app/models/' . $classToLoad . '.class.php');
}
}
spl_autoload_register('autoLoad');
// Determine which function to call based on the url that's listed in the Ajax request
switch($_GET['action'])
{
case 'pageOne':
pageOne();
break;
case 'pageTwo':
pageTwo();
break;
}
function pageOne()
{
$test = new Test();
$test->funcThatReturnStuff();
}
function pageTwo()
{
$test2 = new Test2();
$test2->funcThatReturnStuff();
}
Like I mentioned eariler, if I use a require statement such as:
require('../library/Test.class.php');
$test = new Test();
$test->funcThatReturnStuff();
The class loads and works just fine. But using the same path in the autoloader function throws an error. The really odd thing is if I put an else if statement in the autoloader that loads a class from the folder where my ajaxBootstrap is it also works fine too...
I know I could just use the require statements and be done with the problem but I want to be able to scale the project and not need to use loads of require statements in the future. BTW, I use '../' to get from where my ajaxBootstrap file is to my other folders.
Also, to add to my previous post, I've tried replacing the ../ with absolute paths using define('ROOT', dirname(__FILE__) . '/') and also define('ROOT', $_SERVER['DOCUMENT_ROOT'] . '/path/to/folder/') neither of which worked and still gave me the internal server error in Firebug. In addition, I haven't received any errors in my error log either.
Nevermind... Even after staring at my code for the past few hours I somehow missed the missing period in two of my file paths. I hate coding sometimes... Thank you to anyone who took time to read this.

PHP can not find my class?

How can I specify the exact absolute path?
File is here. I specified it like this:
include "/home/core/public_html/d/core/source/class.Control.php";
Yet it tells me it is not.
*Fatal error: Class 'Control' not found in /home/core/public_html/d/core/source/class.ControlEntry.php on line *
Code:
<?php
class ControlEntry
{
private $control_object;
function __construct( $control_object )
{
$this->control_object = $control_object;
}
public function actuate()
{
if( isset($_POST['ajax_type']) )
{
$this->control_object->ajax( $_POST['ajax_type'] );
}
else
{
$this->control_object->reload();
}
}
}
include "/home/core/public_html/d/core/source/class.Control.php"; // Can not find this f***ing file even though it is there.
$control_entry_object = new ControlEntry( new Control() );
$control_entry_object->actuate();
Troubleshooting List
The error originates from class.ControlEntry.php. Make sure you load class.Control.php before you load ControlEntry.
That is the absolute path. It doesn't look like a bug. Actually it says the Class was not found. It doesn't say the file wasn't found. I guess in your class.ControlEntry.php line 3 references to Control. You need to include the file that contains control before using it.
You're including "/home/domain/public_html/d/core/source/class.Control.php"; but the error says /home/domain/public_html/d/core/source/class.ControlEntry.php. Are you sure you're including the right file or instantiating the right class?

Kohana 3 and SimpleTest using autorun.php

How do I go about integrating Simpletest with Kohana 3? I have checked out this answer but I like to use the autorun.php functionality from SimpleTest.
After some hours of looking over the code, I have discovered how to do it
Create a new copy of index.php, and name it test_index.php
disable the error_reporting line in test_index.php
Create a new copy of bootstrap.php, and name it test_bootstrap.php
comment out the request at the bottom
Ensure that test_index.php includes test_boostrap.php instead of bootstrap.php
Add simpletests to the directory structure
Write the test case - include 'test_index.php' and 'autorun.php' (from simpletests) and code test cases as usual.
My example:
<?php
include_once ("../../test_index.php");
include_once ("../simpletest/autorun.php");
class kohana_init_test extends UnitTestCase
{
function testTrue()
{
$this->assertTrue(true);
}
function testWelcome()
{
$response = Request::factory('main/index')->execute()->response;
$this->assertEqual($response->content, 'testing');
}
}
?>
Some notes: the $response variable depends on if you are using a View or pure text output. If you are using the template controller or a view, then $response is the view which you have used to render the content. Variables in the view are avaliable , as shown above (the variable content is defined inside the view).

Categories