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 !
Related
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 a CRUD app, everything works except updating tags
Here is the update function in my controller
namespace App\Http\Controllers;
use App\Tag;
use App\PageList;
use App\PageListTag;
use Illuminate\Http\Request;
public function update(Request $request, $id)
{
$pages = PageList::find($id);
$pages->pagetitle = $request->get('pagetitle');
$pages->articlelist = $request->get('articlelist');
$pages->status = $request->get('status');
$pages->save();
$pages->tags()->saveMany([
new App\Tag(),
new App\Tag(),
]);
return redirect('/pages')->with('success', 'pages updated!');
}
Here is the Tag model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable = ['page_list_id', 'page_list_tag_id'];
protected $with = ['tag'];
public function tag()
{
return $this->belongsTo('App\PageListTag', 'page_list_tag_id', 'id');
}
}
When I run my app I am getting the following error
Class 'App\Http\Controllers\App\Tag' not found
What am I doing wrong in my code?
You're resolving in the wrong way the models namespaces. Please have a look at the official PHP documentation
In your code you're resolving the Tag class as follows
use App\Tag; // <-- This is right
But in your method you're calling
$pages->tags()->saveMany([
new App\Tag(), // <-- And this is wrong!
new App\Tag(),
]);
You simply have to call new Tag() since the use at the top of your file has already included the class.
Otherwise PHP will try to resolve the class from the current namespace. That's why it's throwing
Class 'App\Http\Controllers\App\Tag' not found
To be right you should have added a \ before App\Tag, so PHP will resolve the class from the root. In this case, the use statement will be useless
Your namespace is App\Http\Controllers, so when you create a tag with the syntax new App\Tag() it is indeed translated into App\Http\Controllers\App\Tag.
So just replace your instructions new App\Tag() with new Tag().
Alternatively, you could also use the absolute notation:
new \App\Tag()
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
So I am having a few dramas with my controllers. They seem to operate properly, however they don't seem to use __construct() at all in any of the controllers.. I'm trying to use this to update our users table to show last activity.
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use App\Helpers\UserHelper;
use App\Helpers\ForumHelper;
use App\Helpers\ShopHelper;
use Auth;
use Image;
use App\User;
use DB;
use Hash;
use File;
class AdminController extends BaseController
{
public function __construct() {
if ($_SERVER["HTTP_CF_CONNECTING_IP"]) {
$_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
if(Auth::check()) {
$time = strtotime("now");
DB::table('users')->where('userid', Auth::user()->userid)->update(['lastactivity' => $time]);
}
}
Any idea what I can do to check and try and get it working again?
I'm running Laravel Framework 5.4.28
A project I did similar on was running Laravel Framework version 5.2.45 and worked fine when I was doing that so I'm confused why this is happening on a newer version.
Any ideas how I can otherwise go about implementing the DB Update when loading stuff from my controllers?
If you want an activity tracker then add a web route middleware:
class ActivityTrackerMiddleware {
public function handle($request,$next) {
if ($request->user()) {
$request->user()->lastactivity = Carbon::now();
$request->user()->save();
}
return $next($request);
}
}
Add this in your web middleware:
'web' => [ ...
StartSession::class,
ActivityTrackerMiddleware::class,
...
];
You can also limit it to the admin controller if you prefer.
So, in L5 I created folder like app/Models/Blog where is file Posts.php which looks like:
<?php namespace App\Models\Blog;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model {
protected $table = 'posts';
}
After it I executed composer dump and then in my controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Models\Blog\Posts as Posts;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
It throws me an error:
FatalErrorException in BlogController.php line 14: Class 'Models\Blog\Posts' not found
Try changing this:
use Models\Blog\Posts as Posts;
To this:
use App\Models\Blog\Posts;
In Laravel 5.2 it's just:
use App\Blog;
or
use App\Blog\Posts;
Change the following
class Posts extends Model {
to
class Posts extends \Eloquent {
You need to check two points :
the namespace have to be in first
the using must be use App\Models\Blog in your case
Like this :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Blog;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
(tested with Laravel 5.4)