Hello. I created the console command in Yii2:
projectDir/commands/SomeController.php
<?php
namespace app\commands;
use yii\console\Controller;
/**
* Class SomeController
* #package app\commands
*/
class SomeController extends Controller
{
public function actionTest()
{
//do something
}
}
I want to call this command in cron,and for testing I try to call it from the console, when I'm in the project folder:
php /var/www/projectDir/yii some/test
Everything works fine. But, if I call this command when I'm in a different directory, I get some errors.
First, i got
ReflectionException: Class app\admin\templates\Generator does not exist in /var/www/projectDir/vendor/yiisoft/yii2/di/Container.php:428
Seeing this, I commented configuration of gii in the file projectDir/common/config/config-console.php
After that I get an error:
Unknown command: some/test
Why is this happening?
I call the command with an absolute path, and it works differently when called from different folders!
You need to use magic constant __DIR__ to build absolute paths. Result of realpath('../../') will depend on path where you run command. You should use
$config['basePath'] = realpath(__DIR__ . '/../../')
or (probably better):
$config['basePath'] = dirname(dirname(__DIR__))
Related
I have a Cron Controller that I would like to run from CPanel's cron job functionality, however my installation does not have an index.php file as I have used mod_rewrite on my .htaccess file so it doesn't show in my url.
I've read the documentation on running it through cli and can only get an input in the error_log using
/usr/local/bin/php /home/user/subdomain.domain.com/app/Controllers/Cron.php
I'm getting the following error:
[05-Oct-2022 21:36:01 UTC] PHP Fatal error: Uncaught Error: Class 'CodeIgniter\Controller' not found in /home/user/subdomain.domain.com/app/Controllers/Cron.php:7
Stack trace:
#0 {main}
thrown in /home/user/subdomain.domain.com/app/Controllers/Cron.php on line 7
This is how my Controller is setup
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\CronModel;
class Cron extends Controller
{
public function __construct(){
$db = db_connect();
$this->cronModel = new CronModel($db);
}
/*
* Function to start cronjob if it's time
*/
public function index(){
$this->cronModel->run();
}
}
No other cli reaches the controller and I keep getting this error.
EDIT: I have this in my Routes file:
$routes->cli('cron/index/(:segment)', 'Cron::index/$1');
I've managed to get it to work. There was a rewrite on the .htaccess file on Codeigniter's root. So basically I added the rewrite rule to the cli path and it worked, like so:
/usr/local/bin/php /home/user/subdomain.domain.com/rewrite_rule_path cron run
I am developing a custom package where I need an access to the config data. I was able to pull data from config through my blade files, however, when I tried to call it from any custom classes I made, it's throwing an error:
Error: Call to undefined function Acme\Package\config()
What's interesting though, is that, when I tried using the facade Illuminate\Support\Facades\Config, it cannot find the class.
Is there any way I could retrieve data from the config (from the package and/or the app)?
<?php
namespace Acme\Package;
class MyClass {
public function test() {
config('app.name');
}
}
UPDATE: It works when running in the browser (package installed in a Laravel project) but fails when running package's test
UPDATE: If this helps, my package can be found here
Call to the config() is from here
And the test case that fails can be found here
<?php
namespace Acme\Package;
class MyClass {
public function test() {
\Illuminate\Support\Facades\Config::get('app.name');
}
}
Try like this, to use the full namespace to the Config Facade. You could also make a use statement under your namespace to inject the facade and then use Config::get('app.name). The reason it is not working is that your package cannot resolve the namespace of that facade as it is outside of the IoC container
You should try this:
<?php
namespace Acme\Package;
use Config;
class MyClass {
public function test() {
Config::get('app.name');
}
}
Updated answer
Please add below line in config/app.php in aliases section
'Config' => Illuminate\Support\Facades\Config::class,
then run below command
php artisan config:cache
php artisan cache:clear
I am using the duncan3dc module in laravel, Successfully importing text from the default example But I do not know how to take a screenshot,
I just installed this module using Composer.
$ composer requires duncan3dc/dusk.
Does it mean that everything needed for this module is automatically installed?
Or do I need something else to take a screenshot?
Below is my controller code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use duncan3dc\Laravel\Dusk;
class TestController extends Controller
{
public function test()
{
$dusk = new Dusk;
$dusk->visit("http://example.com");
$dusk->screenshot("ddd");
echo $dusk->element("h1")->getText() . "\n";
}
}
When I run the above code, I get this error:
file_put_contents (/tmp/ddd.png): failed to open stream: No such file
or directory
I think there is no folder, so I get the error, where should I create the folder in the project folder?
According to the changelog from version 0.8.0 you can pass fully qualified path to screenshot method. So doing this will work (you should see the file ddd.png created near the php file):
$dusk = new Dusk;
$dusk->visit("http://example.com");
$dusk->screenshot("./ddd.png");
echo $dusk->element("h1")->getText() . "\n";
You can also pass absolute path:
$dusk->screenshot("C:\windows\temp\ddd.png"); // if you are using windows
or
$dusk->screenshot("/tmp/ddd.png"); // if you are using macos/linux
Problem I'm currently facing to has been posted here already, yet none of them could solve my one.
I'm talking about database seeder located under url like http://HOSTNAME/laravelfiles/database/seeds/UsersTableSeeder.php. Its content is as follows:
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// What should be being done, it is being done here :)
}
}
Having opened this file directly (by url given above the code), following error is printed:
Fatal error: Class 'Illuminate\Database\Seeder' not found in /var/www/laravelfiles/database/seeds/UsersTableSeeder.php on line 6
I found possible solution. Doesn't work for me:
composer dump-autoload
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
}
NOTE: Even the code above produces the same error.
You must run seeder class via command line, not browser.
you should open your command line and change directory to laravel root folder.
then you need to run command:
php artisan db:seed
and check your database.
for more info please check laravel documentation(Laravel Database Seeding)
I am running the example from the documentation: http://docs.phalconphp.com/en/latest/reference/unit-testing.html#sample-unit-test
I want to create an abstract unit test from Phalcon\Test\UnitTestCase as in the documentation. However when I run my test I become:
PHP Fatal error: Class 'Phalcon\Test\UnitTestCase' not found
I have followed the exact documentation steps. Did anyone have the same problem and solved it?
This class is part of the incubator: https://github.com/phalcon/incubator
$loader = new Phalcon\Loader();
$loader->registerNamespaces(array(
'Phalcon' => '/path/to/incubator/Library/Phalcon/'
));
$loader->register();
I figure it out.
Basically we have to do 2 things.
is whats in the #twistedxtra's answer. (setting up the path to where the incubator is)
in the testsTestUnitTest.php we created, it has the following line
class UnitTest extends \UnitTestCase {
we have to change that line to
class UnitTest extends \Phalcon\Test\UnitTestCase {
What we did was set the proper namespace so that the code knows where the UnitTestCase class is.
Thats it. Cheers...!!!
Make sure you run phpunit command in tests folder. It's very important.
Don't run something like phpunit tests/