Namespace is not working in contoller - php

I have created Mycontoller.php inside app/controllers
class MyController extends BaseController {
public function showWelcome($name){
return "Hello $name !! ";
}
}
When i use inside routes.php
Route::get('user/{name}','App\Controllers\MyController#showWelcome');
It is giving exception
ReflectionException Class App\Controllers\MyController does not exist

just use MyController#showWelcome With the code you showed, you don't have any defined namespaces. You should make it
Route::get('user/{name}','App\Controllers\MyController#showWelcome');

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 external class files

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);
}
}

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;

Yii 2 - using custom helpers

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;

When exactly do you need to import in PHP?

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.

Categories