I'm trying to render PWA directives, but I have this error
Error Class 'App\Services\PWA\MetaService' not found
I have this in AppServiceProvider.php
public function boot()
{
Blade::directive('PWA', function (){
return (new App\Services\PWA\MetaService())->render();
});
}
But the class exists
this is the class
<?php
namespace App\Services;
class MetaService
{
public function render(): string
{
return "<?php \$config = (new \App\Services\PWA\ManifestService)->generate(); echo \$__env->make( 'pwa::meta' , ['config' => \$config])->render(); ?>";
}
}
and that is located on app/Services/PWA
and I have the same problem with ManifestService (undefined class)
The MetaService class namespace must be App\Services\PWA. The path you are pointing to is just a folder structure, namespace is the thing that is important.
Related
Problem
I created service TestService, that I use in colntroller file TestController in function test().
When I called test(), I got an error:
local.ERROR: Class 'App\TestService' not found {"userId":1,"exception":"[object] (Error(code: 0): Class 'App\TestService' not found at /Backend/app/Http/Controllers/TestController.php:8)
Code
TestController.php:
<?php
namespace App\Http\Controllers;
use App\TestService;
class TestController extends Controller
{
public function test()
{
return response()->json(TestService::getTest());
}
}
TestService.php:
<?php
namespace App;
use App\TestService;
class TestService
{
public static function getTest()
{
return "test";
}
}
What I tried
I checked all the names and they are correct.
When I wrote in the colntroller file use App\TestService;
I had autocomplete, so the service and name are visible.
I used these commands to refresh the files: php artisan serve and php artisan clear-compiled.
But it still doesn't work.
You have not correctly defined Namespace.
The namespace must be a directory path where you have created a file.
TestService.php:
<?php
namespace App\Services\Tests;
class TestService
{
public static function getTest()
{
return "test";
}
}
TestController.php:
<?php
namespace App\Http\Controllers;
use App\Services\Tests\TestService;
class TestController extends Controller
{
public function test()
{
//Call service class like.
return response()->json(TestService::getTest());
}
}
I have some repetitive codes inside my laravel(7.23.0)controller
use App\ModelA;
use App\ModelB;
use App\ModelC;
use App\Traits\DbTrait;
class DarsController extends Controller
{
use DbTrait;
public function A($id) {
return ModelA::where('column', $id)->get(*);
}
public function B($id) {
return ModelB::where('column', $id)->get(*);
}
public function C($id){
return ModelC::where('column', $id)->get(*);
}
//the only difference in these codes is model, all codes are the same
}
I had created a folder named Traits and inside that I had defined a trait DbTrait.php
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName , $id){
return $ModelName::where('column', $id)->get('*');
}
}
so I modified my controller's functions to this
public function A($id) {
// return ModelA::where('column', $id)->get(*); works fine
$this->getAllz('ModelA', $id);// throws an error
}
it throws an error message: "Class 'ModelA' not found"
thank you
update:
i should apologize, i am really sorry, 3 of the answer worked, and i see the data inside network tab,i am using vue to display data,
and i think using trait made a complex array
this is my simple vue
axios.get('/api/emla/' + id).then(response =>{
this.data = JSON.parse(JSON.stringify(response.data));
}
Import the classes which will be used in your trait as you have done in the controller class.
use App\ModelA;
use App\ModelB;
use App\ModelC;
the reason you're getting Class 'ModelA' not found is because it's looking for that class in your traits directory, which it won't find. so you need this instead:
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName , $id) {
return app( "\App\\" . $ModelName )::where('column', $id)->get('*');
}
}
assuming that you've defined your models under \App namespace
you need to modify your trait
ModelName::where it try to load the class in trait so use $ModelName->where here $ModelName is already instance of that class so you can call function via -> operator
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName ,$columnName ,$id){
return $ModelName->where($columnName, $id)->get('*');
}
}
and to call this function
$this->getAllz(new ModelA, $id);
this way you don't need to import class inside trait
your final code will be like this
use App\ModelA;
use App\ModelB;
use App\ModelC;
use App\Traits\DbTrait;
class DarsController extends Controller
{
use DbTrait;
public function A($id)
{
return $this->getAllz(new ModelA,'column', $id);
}
public function B($id)
{
return $this->getAllz(new ModelB,'column', $id);
}
public function C($id)
{
return $this->getAllz(new ModelC,'column', $id);
}
}
100% should work if you follow this
public function A($id) {
// return ModelA::where('column', $id)->get(*); works fine
$model = new ModelName(); // new ModelName;
$this->getAllz($model, $id);// throws an error
}
public function getAllz($ModelName , $id){
return $ModelName->where('column', $id)->get('*');
}
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;
I need use a PHP function to convert numbers to letters. I create a file named: converter.php in the folder “Controller”. I need call the function named “convertir_numero_letras($number)” insert in converter.php.
I call the function “convertir_numero_letras($number)” from the following controller:
public function convertirAction()
{
$number=1234;
$this->convertir_numero_letras($number);
return $this->render('contratos/mostrar_cifra.html.twig', array('numero_convertido' => $numero_convertido));
}
But this code not work. I obtain the following
error message:
Attempted to call an undefined method named "convertir_numero_letras" of class "BackendBundle\Controller\ContratosController".
As I can fix this?
you can create a folder inside bundle like yourBundel/Helper
and create you custom class in folder like ConverHelper.php
like this :
class ConvertHelper
{
public static function numero_letras($param)
{
return 'converted value';
}
}
and then you can call in any controller and any bundle like this:
yourBundle\helper\ConverterHelper::numero_letras('someting');
don't forget to add namespace of ConverterHelper to your contoller file
example:
// file: GRF/BlogBundle/Helper/ConverterHelper.php
<?php
namespace GRF\BlogBundle\Helper;
class ConverterHelper
{
public static function toNum($param)
{
return $param;
}
}
and usage in controller:
//file controller
<?php
namespace GRF\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PostController extends Controller
{
public function indexAction()
{
return new Response(\GRF\BlogBundle\Helper\ConverterHelper::toNum(32434));
}
}
Sorry for the English, but I am using the google translator.
First of all I leave my code:
FtpServiceProdiver.php
<?php namespace Jaimemse\Ftp;
use Illuminate\Support\ServiceProvider;
class FtpServiceProvider extends ServiceProvider {
protected $defer = false;
public function boot()
{
$this->package('jaimemse/ftp');
}
public function register()
{
$this->app->bind('ftp', function()
{
return new Ftp;
});
}
public function provides()
{
return array();
}
}
Ftp.php (the class)
<?php namespace Jaimemse\Ftp;
class Ftp {
public function hello()
{
return 'hola';
}
}
Facades/Ftp.php (Facade)
<?php namespace Jaimemse\Ftp\Facades;
use Illuminate\Support\Facades\Facade;
class Ftp extends Facade {
protected static function getFacadeAccessor() { return 'ftp'; }
}
app.php
'Jaimemse\Ftp\FtpServiceProvider',
'Ftp' => 'Jaimemse\Ftp\Facades\Ftp',
If instead of that Facade put this, if it works:
'Ftp' => 'Jaimemse\Ftp\Ftp',
The problem I have is that when using the alias in the file app.php seeks Ftp class in the folder Facades/Ftp.php
Call to undefined method Jaimemse\Ftp\Facades\Ftp::hello()
Someone can help me? Thanks!
You have to extend the BaseController:
<?php namespace Jaimemse\Ftp;
class Ftp extends \BaseController {
public function hello()
{
return 'hola';
}
}
Also your route should be (with namespace):
Route::get('/ftp', 'Jaimemse\Ftp\Ftp#hello');
Also
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\ServiceProvider;
should be
use \Illuminate\Support\Facades\Facade;
use \Illuminate\Support\ServiceProvider;
You should put in app.php
'Jaimemse\Ftp\FtpServiceProvider', in 'providers' array (before 'aliases')
and in 'aliases' array
'Ftp' => 'Jaimemse\Ftp\Facades\Ftp',
I fixed it by adding in register method:
FtpServiceProvider.php
public function register()
{
$this->app->bind('ftp', function()
{
return new Ftp;
});
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Ftp', 'Jaimemse\Ftp\Ftp');
});
}
Ftp.php
class Ftp {
public function hello()
{
return 'hello';
}
}
App.php
'Jaimemse\Ftp\FtpServiceProvider',
I have not added any app.php alias in the file. I deleted Facade file.
Now I can do things like:
Ftp::hello();
Hope that helps someone. Thank you!