When trying to start the development server under Symfony 4
(i.e. php bin/console server start), I get the following error:
The autoloader expected class
"App\Hs\LuckyNumberBundle\Controller\LuckyNumberController" to be
defined in file "/home/rainermusik/musikdb/vendor/composer/../..
/src/Hs/LuckyNumberBundle/Controller/LuckyNumberController.php".
The file was found but the class was not in it, the class name or namespace probably has a typo.
the controller-class in question is defined as follows:
// src/Controller/LuckyNumberController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LuckyNumberController extends Controller
{
public function number()
{
$number = mt_rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
I can't see what I am doing that's incorrect.
Related
I'm using codeigniter 4 but when I try to call my own library I get the error Undefined variable: utils.
here's my code:
/app/libraries/Utils.php
<?php
namespace App\Libraries;
class Utils
{
function generateRandomString($length = 10) {
return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
}
/app/Controllers/Users.php
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
use App\Models\UserModel;
use App\Libraries\Utils;
class Users extends ResourceController
{
...
public function do_reset_password()
{
$utils = new Utils();
$str = $utils->generateRandomString(); // the error points to this line
...
You should place your file Utils.php in /app/Libraries
Do not create your own libraries directory in root project.
In your use line what happens if you do this:
use App\Libraries\Utils as MyUtils; and call $utils = new MyUtils instead?
Please also make sure your Libraries folder begins with a capital L.
I want to create a directory for utils in Laravel 8. I have this but it doesn't work:
app/Utils/DateTime.php:
<?php
namespace App\Utils\DateTime;
const ISO8601_DATE_FORMAT = "Y-m-d\TH:i:s.uP";
function parseISO8601(string $time): \DateTime {
if ($time.endsWith("Z")) {
$time = $time.str_replace($time, "Z", "+00:00");
}
return \DateTime::createFromFormat(ISO8601_DATE_FORMAT, $time);
}
app/Http/Controllers/SearchController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
use function App\Utils\DateTime\parseISO8601 as parseISO8601;
class SearchController extends Controller
{
public function run(Request $request)
{
parseISO8601("2021-10-03T10:00:45.145126+01:00");
}
}
But I get an error:
Call to undefined function App\Utils\DateTime\parseISO8601()
What am I missing? It seems that it can't autoload DateTime for some reason. Do I need to manually configure an extra path somewhere in Laravel?
I changed it to a class and restarted PHP. It then gave me an error:
Class App\Utils\DateTime\DateTime located in ./app/Utils/DateTime.php does not comply with psr-4 autoloading standard. Skipping. which is explained here
I had incorrectly specified the namespace as App\Utils\DateTime even though the file was called DateTime.php. I just needed to change the namespace to App\Utils.
I have the following code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Enemy extends Model
{
// ...
static function fight($id)
{
if(Enemy::calcDist($id))
{
$model = Enemy::find($id);
if($model->status == 1)
{
$model->status = 2;
$model->save();
}
}
}
}
When I try to do App\Enemy::fight(1) in php tinker it shows error:
"Class 'App\App\Enemy' not found".
I tried with "calcDist($id)", with "self::calcDist($id)", also at find($id) function, but no result.
How I can solve this?
Edit: I found the problem; that error comes from another part of code...
When you are in namespace App you dont need to use App\Enemy in your call.
Simply use Enemy::fight(1), or use the absolute namespace \App\Enemy::fight(1)
When you use a static class by his name, the engine search the class into the current namespace. If no namespace is given, then it uses the namespace "\".
namespace App;
Enemy::fight(1); // \App\Enemy::fight(1) ok
App\Enemy::fight(1); // \App\App\Enemy::fight(1) wrong
I am trying to create an ACL component as a service, for a multi-module PhalconPHP application. When I call the ACL component from the Controller Base, I am getting an error that I can't re-declare the ACL class.
Any ideas how to fix it, and understand the logic of why it is re-initialized again?
Fatal error: Cannot declare class X\Acl\Acl because the name is already in use in C:\xampp\htdocs\x\app\common\Acl\Acl.php on line 12
Update:
If I changed everything to "Pacl" then it works. I assume there might be a mixup with the PhalconPHP namespace. I am either not using the namespaces properly, or there's a bug in PhalconPHP 2.1 Beta 2.
/app/common/Acl/Acl.php
namespace X\Acl;
use Phalcon\Mvc\User\Component;
use Phalcon\Acl;
use Phalcon\Acl\Role as AclRole;
use Phalcon\Acl\Resource as AclResource;
/*
* ACL component
*/
class Acl extends Component {
private function initialize() {
}
public function isAllowed() {
die('called');
}
}
/app/front/controllers/ControllerBase.php
namespace X\Front\Controllers;
use Phalcon\Session as Session;
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Dispatcher;
class ControllerBase extends Controller {
public function beforeExecuteRoute(Dispatcher $dispatcher) {
//$this->acl = $this->getDI()->get("acl");
var_dump($this->acl->isAllowed()); //same behavior in both case
}
}
/app/front/Module.php
namespace X\Front;
use Phalcon\DiInterface;
use Phalcon\Mvc\Dispatcher;
use X\Acl\Acl as Acl;
class Module {
public function registerAutoloaders() {
$loader = new \Phalcon\Loader();
$loader->registerNamespaces(array(
'X\Front\Controllers' => __DIR__ . '/controllers/',
'X\Front' => __DIR__,
'X' => __DIR__ . '/../common/'
));
$loader->register();
}
public function registerServices(DiInterface $di) {
$di['acl'] = function() {
return new Acl();
};
}
}
This is not Phalcon issue. Look closely at your code:
namespace X\Acl;
use Phalcon\Acl;
class Acl extends ... {
}
What Acl interpreter should use? X\Acl\Acl or Phalcon\Acl?
The same error you get for example for the following code
namespace My\Awesome\Ns;
use Some\Name; # Name 1
class Name # Name 2
{
}
My application file:
<?php // /src/app.php
require_once __DIR__ . '/../lib/vendor/Sensio/silex.phar';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Foo\Bar;
$app = new Silex\Application();
$app['autoloader']->registerNamespace('Foo', __DIR__);
$bar = new Bar();
(...)
My Bar class:
<?php /src/Bar.php
namespace Foo;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Response;
class Bar implements ControllerProviderInterface { ... }
When I do a $bar = new Bar() in my app.php, I get an error: Fatal error: Class 'Moken\Classname' not found in (...)/src/app.php on line 11
Can anyone tell me what I am doing wrong?
If you use namespace Foo; you must locate this class in Foo directory
Every namespace part is a directory in symfony
If not works, you must show the loader where to find this class
In symfony2 I use for this:
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
// HERE LOCATED FRAMEWORK SPECIFIED PATHS
// app namespaces
'Foo' => __DIR__ . '/../src',
));
In your main php file (index.php) you must:
declare the use of your Controller Provider;
after creation of your Application object you must register your namespace;
mount your Controller Provider.
For example (Example\Controllers is the namespace and XyzControllerProvider is the Controller Provider, the url is /my/example):
[...]
// declare the use of your Controller Provider
use Example\Controllers\XyzControllerProvider;
[...]
//after creation of your Application object you must register your namespace;
$app = Application();
$app['autoloader']->registerNamespace('Example', __DIR__.'/src');
[...]
//mount your Controller Provider
$app->mount('/my/example', new Example\Controllers\XyzControllerProvider());
the Controller Provider (under src/example/controllers) will be:
<?php
namespace Example\Controllers;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
class XyzControllerProvider implements ControllerProviderInterface {
public function connect(Application $app) {
$controllers = new ControllerCollection();
$controllers->get('/', function (Application $app) {
return "DONE;"
});
return $controllers;
}
}