How to create a utils directory in laravel? - php

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.

Related

Interface 'App\additional\IManage' not found

I am trying to create interface then implementing it so I created folder inside the app folder called "additional" and created inside it file called IManage.php inside it the following:
<?php
namespace App\additional;
interface IManage{
publice function manageCycle(int $c):void;
}
then I created a file called manage.php inside the "additional" folder as follow:
<?php
namespace App\additional;
class Manage implements IManage{
public function manageCycle(int $c){
echo $c;
}
but when I try to call the create new object of the Manage class and call manageCycle method inside any controller function I get:
"Interface 'App\additional\IManage' not found"
how to solve this please?
There are some typo errors and there is return type issue I solved please check this
IManage.php
namespace App\Http\Controllers\Admin;
interface IManage
{
public function manageCycle(int $c);
}
Manage.php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class Manage implements IManage
{
public function manageCycle(int $c){
}
}

Laravel : Class 'App\Services\OvhApiHandlerClass' not found

I'm starting Laravel and i'm trying to create an external class to use like a 'Library'.
I've searched a lot and came up with this solution :
I created a folder 'Services' in 'App' and made a class file like so :
App/Services/OvhApiHandlerClass.php
This file looks like so :
<?
namespace App\Services;
use App\OvhBill;
use App\OvhBillDetail;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Ovh\Api;
class OvhApiHandlerClass
{
public function ovh_get_bills(string $consumer_key, $from = null, $to = null)
{
// Do something
}
}
So now i want to use this class inside a controller.
But i'm getting an error saying my class does not exist.
Here is my Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Services\OvhApiHandlerClass;
class OperationsController extends Controller
{
public function index()
{
$from = new \Carbon\Carbon('first day of January 2019');
$ovhHandler = new OvhApiHandlerClass();
$ovhHandler->ovh_get_bills('K87u3410p89ijKLao', $from);
return view('operations.index');
}
}
I already did, of course
composer dump-autoload
I'm kinda lost, what am i missing?
Thank you very much for your time !

Starting Symfony 4 development Server does not work

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.

How to solve namespaces problems in php tinker?

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

laravel is giving error on custom controller used in routes

I have a simple in routes/web.php file
Route::get(Config::get('constants.ADMIN_PATH') . '/categories', 'AdminControllers\AdminPagesController#index');
I have made a folder AdminControllers and inside that there is a controller named AdminPagesController but i am getting error as
Class App\Http\Controllers\AdminControllers\AdminPagesController does not exist
Whereas i looked into the same folder and class exist. Here is my class code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminPagesController extends Controller
{
public function __construct() {
}
public function index () {
return "hello";
}
}
Change you namespace to
namespace App\Http\Controllers\AdminControllers;
Laravel will resolve controllers based on your name spacing, not on your directory structure.
You should specify the namespace correctly, change it to:
namespace App\Http\Controllers\AdminControllers; // <------- correct this namespace
use Illuminate\Http\Request;
class AdminPagesController extends Controller
{
public function __construct() {
}
public function index () {
return "hello";
}
}
Hope this helps!
If you choose to nest your controllers deeper into the **
App\Http\Controllers
** directory, use the specific class name relative to the
App\Http\Controllers
root namespace.
namespace App\Http\Controllers\AdminControllers;

Categories