I'm trying to use a custom helper class which i create under frontend/components/Helper (Helper.php)
The content of that file is something like:
<?php
namespace frontend\components\Helper;
class Helper {
public static function helperGreetings() {
echo("hello helper");
}
}
?>
and on my SiteController.php i have the following:
use frontend\components\Helper;
class SiteController extends Controller
{
public function actionIndex()
{
Helper::helperGreetings();
return $this->render('index');
}
}
What should i do to have it working?
BTW, the error i get is Unknown Class – yii\base\UnknownClassException
Unable to find 'frontend\components\Helper' in file: /Users/foo/sites/bar.dev/frontend/components/Helper.php. Namespace missing?
Change the namespace in the Helper class from
namespace frontend\components\Helper;
to
namespace frontend\components;
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'm trying to redirect to an external URL from a helper class.
Here's my controller:
<?php
namespace App\Http\Controllers;
use App\Lead;
use Illuminate\Http\Request;
use App;
use Helper;
class MyController extends Controller
{
public function entities_get() {
Helper::my_function(); // <---- Call my Helper class method to redirect.
return view( 'template' );
}
}
Here's my helper class used in the controller:
<?php
namespace App\Helpers;
class Helper
{
public static function my_function()
{
return redirect()->away( 'https://www.google.com' ); // <---- Not redirecting.
}
}
Why is the redirect() not working inside my_function()? Do I need to include some Laravel classes using the PHP "use" statement?
You can add send method and it will work.
<?php
namespace App\Helpers;
class Helper
{
public static function my_function()
{
return redirect()->away('https://www.google.com')->send();
}
}
I have a file that i put in app\Classes\myVendor\dev_client_api.php. This file has a class in it:
class someClass{
//stuff
}
I want to use this class in a controller.
In my controller I have done the following:
namespace App\Classes\myVendor;
use dev_client_api;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
When i execute this page I get:
Class 'App\Classes\myVendor\Controller' not found
I have to admit I am not sure what exactly I am doing. Any help would be great.
I assume your Controllers are in Laravel's default App\Http\Controller directory.
namespace App\Classes\myVendor;
class someClass {
//stuff
}
namespace App\Http\Controllers;
use App\Classes\myVendor\someClass;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
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;
When autoloading classes, the following runs without a problem:
<?php
namespace App\Resources;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
How does this work? I never imported the Controller class:
<?php
namespace App\Resources;
use App\Resources\Controller;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
If you use non-qualified class name (without the namespace), PHP assumes you mean the current namespace. The code above works because both Home and Controller are in the same namespace App\Resources.