When putting Employee.php class in the same directory with index.php every thing is working fine
index.php:
<?php
$f3=require('lib/base.php');
$f3->config('config.ini');
$f3->map('/employee','Employee');
$f3->run();
When putting Employee.php class under a directory, app for examle and running the following index.php
<?php
$f3=require('lib/base.php');
$f3->config('config.ini');
$f3->map('/employee','app\Employee');
$f3->run();
I am getting an error
Internal Server Error
Fatal error: Cannot redeclare class Employee
• /home/zaky/Development/kinder/app/Employee.php:2
Employee.php:
<?php
class Employee{
function get() {die("get---");}
function post() {die("post---");}
function put() {die("put---");}
function delete() {die("delete---");}
}
Related
i am trying to use amazonpay and encounter the following problem;
when i if ($whatever) require_once("amazonpay_stuff.php") with content:
namespace AmazonPay
require_once "apay/config.php";
include 'apay/amazon-pay.phar';
and then in the same file where i have the require i try to use
$client = new Client($amazonpay_config);
this does not work and i get error message
Fatal error: Class 'Client' not found in ....
when i have all that code in one file;
namespace AmazonPay
require_once "apay/config.php";
include 'apay/amazon-pay.phar';
$client = new Client($amazonpay_config);
it works fine - how can i make it work with require_once?
I am trying to run PHPUnit_Framework_TestCase on Yii2-advanced. Installed it via composer in folder PHPUnit in the root directory. From there I tried to test with simple test but what I get is an error:
phpunit TestLogin.php
PHP Fatal error: Class 'dektrium\user\models\LoginForm' not found in Z:\toma\toma-metropizza\htdocs\frontend\models\LoginForm.php on line 8
Fatal error: Class 'dektrium\user\models\LoginForm' not found in Z:\toma\toma-metropizza\htdocs\frontend\models\LoginForm.php on line 8
This my TestLogin.php file from the PHPUnit directroy:
<?php
require_once "vendor/autoload.php";
require_once "../frontend/models/LoginForm.php";
class TestLogin extends PHPUnit\Framework\TestCase
{
public function setUp()
{
$login = new LoginForm();
}
public function testStatic()
{
$this->assertClassHasStaticAttribute('test', 'LoginForm');
}
}
This is my LoginForm.php:
<?php
namespace frontend\models;
use Yii;
use \dektrium\user\models\LoginForm as BaseLoginForm;
class LoginForm extends BaseLoginForm
{
public static $test = [];
public function attributeLabels()
{
return [
'login' => Yii::$app->OutData->getLabel(208),
'password' => Yii::$app->OutData->getLabel(98),
'rememberMe' => Yii::t('app','app.Remember me'),
];
}
}
It extends the base dektrium user model. But it seems like the test can't find the base model or something. Can you give me advice? I don't want to use the Yii2 build in tests. Want to write my own. Thank you in advance!
EDIT Directroy structure:
app/
/frontned
/models
LoginForm.php
/PHPUnit
/vendor
composer.json
conposer.lock
TestLogin.php
/vendor
etc.
I created a service provider named AdminServiceProvider
namespace App\Providers;
use Modules\Orders\Models\Orders;
use Illuminate\Support\ServiceProvider;
use View;
class AdminServiceProvider extends ServiceProvider
{
public function boot()
{
$comments = Orders::get_new_comments();
View::share('comments', $comments);
}
public function register()
{
}
}
Registered the provider
App\Providers\AdminServiceProvider::class,
Now I try to attach it to the controller
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Providers\AdminServiceProvider;
class AdminController extends Controller
{
public $lang;
public function __construct()
{
}
public function index(){
return view('admin/dashboard');
}
}
Now I get this error message
Undefined variable: comments
This is the first time I try to use a custom service provider and don't know exactly how it works I'm sure there's something missing Hope you can help. Thanks in advance.
[UPDATE]
removed use App\Providers\AdminServiceProvider; from the controller
php artisan clear-compiled solved the problem but I want to attach it to some controllers not all controllers as the $comments are sent to all contollers in my app. So how to attach the service provider to specific controllers not all of them?
For the undefined variable run: php artisan clear-compiled will solve it
If you want to share a variable in some of your views you can create a middleware and assign it to the views you want to share the data with:
First create a middleware: php artisan make:middleware someName
Then in the handle function you add your view sharing logic:
$comments = Orders::get_new_comments();
view()->share('comments',$comments);
return $next($request);
Then register your middleware under the $routeMiddleware array and
give it an alias.
Then attach it to your routes like:
Route::group(['middleware'=> 'yourMiddlewwareName'], function(){
//your routes
});
If you have all your admin views in one directory (views\admin for example) you can use view composer in AdminServiceProvider:
public function boot()
{
view()->composer('admin.*', function($view){
$view->with('comments', Orders::get_new_comments());
});
}
It will attach comments variable to each view in your views\admin directory.
You can also attach a variable to some specific views or folders like this:
view()->composer(['admin.posts.*', 'admin.pages.index'], function($view){
$view->with('comments', Orders::get_new_comments());
});
i make simple custom Hellowidget .
HelloWidget class and file put in "components" directory and component directory put in "root directory in application"
`
public $message;
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = 'Hello World';
}
}
public function run()
{
return Html::encode($this->message);
}
`
when this widget call in views
\app\components\HelloWidget::widget(['message' => 'Good morning'])
so i am getting error "Class 'app\components\HelloWidget' not found" namespace add still getting error
any idea about that widget
Hello i found solution
if you work on yii2 advanced template so components can`t put in "root directory" if you put so class not found error get
so solution is if you work on "frontend" so components put in that directory then used custom widget it will works ;)
This is the problem: I haven't be able to load a class from a namespace.
I'm developing a restful app and I'm trying to follow the Entity/Service/Repository way to access and give format to the requested data. The thing is that I cannot load any class from Services. I have created a folder inside my app called api, and within it the others 3 folders: api/entities/, api/services/ and api/repositories/. There are 2 more folders inside services: validators and datapickers.
As it is a RESTulf app, I also created an api folder inside the controllers folde: controllers/api/.
Here is the the current tree of my app folder:
app/
api/
entities/
repositories/
services/
datapickers/
MemberData.php
ConsumeData.php
validators/
...
models/
controllers/
api/
members.php
(other restful controllers)
languages/
...
In first instance, this is my Autoload section from start.php:
Autoloader::directories(array(
path('app').'models',
path('app').'libraries',
path('app').'api'
));
Autoloader::namespaces(array(
'Api' => path('app').'api',
));
And this is MemberData.php:
<?php
namespace Api\Services\Datapickers;
use Api\Repositories as Repo;
class MemberData
{
/* Do some stuff */
}
Now, when I try to use MemberData in controllers/api/members.php:
<?php
use Api\Services\Datapickers\MemberData;
class Api_Members_Controller extends Api_Base_Controller
{
public function get_index($id = null)
{
if (!empty($id))
$this->params->data = MemberData::getById($id);
else
{
$this->params->data = MemberData::getAll(
Input::get('offset'),
Input::get('limit')
);
}
return Response::json($this->params, 200);
}
}
I get the following:
Unhandled Exception
Message:
Class 'Api\Services\Datapickers\MemberData' not found
Location:
/path_to_project/application/controllers/api/members.php on line 15
which is this line: $this->params->data = MemberData::getById($id);
I autoload the api folder and register the base api namespace in start.php but I still keep receiving the same error. IT's like Laravel doesn't recognize the api namespace or something like that.
I tried to register the full namespace:
Autoloader::namespaces(array(
'Api\Services\Datapickers' => path('app').'api/services/datapickers',
));
but the error I got was: Call to undefinde method 'Api\Services\Datapickers\MemberData::getById()'. This was just a test (I don't want to register every sub-namespace in the Autoloader).