I don't know what going on, I was setting my mongodb before. then I refresh my page and suddenly there was an error notification
asking for namespace missing
Unable to find 'application\modules\home\models\User' in file: F:\aplikasi\laragon\www\yiiad\application/modules/home/models/User.php. Namespace missing?
I have already checked the codes I made before, and still don't know where my mistakes
This is my models user structure
\application\modules\home\models\user
this models\user code
<?php
namespace home\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{}
?>
My Alias
<?php
Yii::setAlias('#modules', dirname(dirname(__DIR__)) . '/application/modules');
My Path setting
'basePath' => '#modules/home',
'modules' => [
'admin' => [
'class' => 'admin\Module'
],
'home' => [
'class' => 'home\Module'
],
],
My modules
<?php
namespace home;
class Module extends \yii\base\Module{
public function init()
{
parent::init();
if (\Yii::$app instanceof \yii\console\Application) {
$this->controllerNamespace = 'home\controllers';
}
}
}
Namespace home is incorrect. You should use modules alias in the namespace of the module
namespace modules\home;
class Module extends \yii\base\Module
{
}
and in config
'modules' => [
'home' => [
'class' => 'modules\home\Module'
],
],
Or you must set alias for home directory
'aliases' => [
'#home' => 'path to home directory'
],
'modules' => [
'home' => [
'class' => 'home\Module'
],
],
Related
I am a newbie in yii2. I want to use roxymce file manager in my yii2 project. I followed these docs for use in yii but when using this section, I get Undefined variable: form error and when use ActiveForm::begin() in I get Getting unknown property: navatech\roxymce\models\UploadForm::thumb error. I want to know when I must use that's controllers in my project. This code for my fileupload view:
<?php
use yii\widgets\ActiveForm;
use yii\web\View;
use yii\helpers\Url;
$this->title = Yii::t('app', 'Upload Course Files');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Presented Courses'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], 'action' => '#']); ?>
<?php $form->field($model, 'file')->fileInput(['id' => 'fieldID1'])->label(false) ?>
<a href="<?=Url::to([
'/roxymce/default',
'type' => 'media',
'input' => 'fieldID1',
'dialog' => 'iframe',
]) ?>" id="fileup" class="fancybox" ><i class="fa fa-upload"></i></a>
<?php ActiveForm::end();?>
<?php
$this->registerJsFile('#web/js/jquery.fancybox.min.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerCssFile('#web/css/jquery.fancybox.min.css', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJs('
$("#fileup").fancybox({ type: "iframe"});
', View::POS_END);
?>
And this code is for the controller of this view(fileupload):
public function actionUploadfile()
{
$model = new UploadForm();
return $this->render('uploadfile',['model'=>$model]);
}
And this my main config file in backend directory:
'modules' => [
'roxymce' => [
'class' => 'navatech\roxymce\Module',
'uploadFolder' => '#app/web/uploads/images',
'uploadUrl' => '/uploads/images',
],
],
I used yii2 advancd template. If anyone used this module, please hint me.
Simply becouse the variable $form is not defined if you use the code like int the example. In your view when you use the extension you need to use ActiveForm::begin() like this:
Simple example
<?php
$form = ActiveForm::begin(['id' => 'roxymce-form']);
echo $form->field($model, 'thumb')->textInput(['id' => 'fieldID'])->label(false);
?>
<a href="<?= \yii\helpers\Url::to([
'/roxymce/default',
'type' => 'image',
'input' => 'fieldID',
'dialog' => 'fancybox',
]) ?>"><i class="fa fa-upload"></i></a>
<script>
$("a").fancybox();
</script>
<?php ActiveForm::end(); ?>
[Edit]
It was pretty hard to run it but in the end I succeeded. I use basic template of Yii2.
Start with install package with composer like in the guide. After this create the classes of assets bundles to publish javascript packages. My assets bundles are these and they are locate in applicationroot/assets folder:
assets/AppAsset.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
public $jsOptions = [
'position' => \yii\web\View::POS_HEAD,
];
}
assets/FancyBoxAsset.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class FancyBoxAsset extends AssetBundle
{
public $sourcePath = '#bower/fancybox/source';
public $js = [
'jquery.fancybox.pack.js',
];
}
assets/FontAwesomeAsset.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class FontAwesomeAsset extends AssetBundle
{
public $sourcePath = '#bower/fontawesome';
public $css = [
'css/font-awesome.min.css',
];
}
assets/LazyLoadAsset.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class LazyLoadAsset extends AssetBundle
{
public $sourcePath = '#bower/jquery.lazyload';
}
assets/PatternflyTreeviewAsset.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class PatternflyTreeviewAsset extends AssetBundle
{
public $sourcePath = '#bower/patternfly-bootstrap-treeview/dist';
public $css = [
'bootstrap-treeview.css',
];
public $js = [
'bootstrap-treeview.js',
];
}
assets/TinymceAsset.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class TinymceAsset extends AssetBundle
{
public $sourcePath = '#bower/tinymce';
public $jsOptions = [
'position' => \yii\web\View::POS_HEAD,
];
}
Now you must to add in your config file this lines of code:
config/web.php
return [
'id' => 'basic',
'basePath' => dirname(__DIR__),
...
'modules' => [
'roxymce' => [
'class' => 'navatech\roxymce\Module',
'uploadFolder' => '#app/web/uploads/images',
'uploadUrl' => '../uploads/images',
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>/' => '<module>/<controller>/<action>',
],
]
...
];
After you need to configure your application for work in a clean URL context. Follow this for more information about: Enable clean URL in Yii2
Now you can finally fully use the plugin in all its contexts. There are two methods for use it:
Integrated with TinyMce
views/site/tinymceIntegrated.php
<?php
use \app\assets;
assets\FontAwesomeAsset::register($this);
assets\LazyLoadAsset::register($this);
assets\FancyBoxAsset::register($this);
assets\PatternflyTreeviewAsset::register($this);
assets\TinymceAsset::register($this);
// Include ActiveRecord Model
echo \navatech\roxymce\widgets\RoxyMceWidget::widget([
'model' => app\models\YourModel::findOne(1),
'attribute' => 'content',
]);
// Sample HTML without ActiveRecord Model
echo \navatech\roxymce\widgets\RoxyMceWidget::widget([
'name' => 'Post[content]',
]);
Without TinyMce
views/site/tinymceWithout.php
<?php
use yii\helpers\Html;
use \app\assets;
assets\FontAwesomeAsset::register($this);
assets\LazyLoadAsset::register($this);
assets\FancyBoxAsset::register($this);
assets\PatternflyTreeviewAsset::register($this);
//assets\TinymceAsset::register($this);
$js = <<<JS
$("a").fancybox();
JS;
$this->registerJs($js, \yii\web\View::POS_READY, 'upload-handler');
?>
<a href="<?= \yii\helpers\Url::to([
'/roxymce/default',
'type' => 'image',
'dialog' => 'fancybox',
]) ?>"><i class="fa fa-upload"></i></a>
With this configuration for me work.
I've a controller Controller\Api\ProductController for rest call and it's defined in module.config.php
'controllers' => [
'factories' => [
Controller\Api\ProductController::class => function($container) {
return new Controller\Api\ProductController(
$container->get(\Commerce\Model\Product::class), $container->get(\Commerce\Controller\Plugin\ProductPlugin::class)
);
}
]
]
In the above code you can see I'm injecting a plugin class \Commerce\Controller\Plugin\ProductPlugin::class which is defined in module.config.php
'controller_plugins' => [
'factories' => [
Controller\Plugin\ProductPlugin::class => InvokableFactory::class,
],
'aliases' => [
'product' => Controller\Plugin\ProductPlugin::class,
]
]
Now when I'm hitting the rest url it shows error message
Unable to resolve service "Commerce\Controller\Plugin\ProductPlugin" to a
factory; are you certain you provided it during configuration?
What I'm missing ?
Plugin code is
<?php
namespace Commerce\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
class ProductPlugin extends AbstractPlugin
{
//....
}
Controller plugins do not get injected to the controller.
Remove
$container->get(\Commerce\Controller\Plugin\ProductPlugin::class)
from the factory callback and also remove the 2nd parameter from the constructor of your ProductController
To use the plugin, just do:
$plugin = $this->plugin(Plugin\ProductPlugin::class);
or
// using the alias
$plugin = $this->product();
in your action controllers.
https://docs.zendframework.com/zend-mvc/plugins/
Controller plugin example
I'm using a Base controller and I had to inject plugins through dependencies
based on the request.
Above configuration was fine. All I had to do is define my plugin in service_manager section.
'service_manager' => [
'factories' => [
Controller\Plugin\ProductPlugin::class => function($sm) {
$dependencies = /////
$model = new \Commerce\Model\Product($dependencies);
return new Controller\Plugin\ProductPlugin($model);
},
\Commerce\Model\Product::class => function($sm) {
$dependencies = /////
return new \Commerce\Model\Product($dependencies);
}
],
],
Why do you still need to say:
use MyClass\Table\Facades\Table;
at the top of a laravel controller
even if you have specified it in app/config/app.php
'aliases' => [
'Table' => MyClass\Table\Facades\Table::class,
You don't?
'aliases' => [
'MyClass' => Some\Vendor\Something\Facades\MyClass::class,
]
Then you can do
use Myclass;
class MyController extends Controller {
public function fetch($id) {
return MyClass::find(1)
}
I am having the following error
InvalidArgumentException in FormBuilder.php line 39:
Form class with name App\Http\Controllers\App\Forms\SongForm does not exist.
on Laravel,
SongsController.php class
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;
class SongsController extends BaseController {
public function create(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
return view('song.create', compact('form'));
}
public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class);
if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}
// Do saving and other things...
}
}
SongForm.php
<?php
namespace App\Forms;
use Kris\LaravelFormBuilder\Form;
class SongForm extends Form
{
public function buildForm()
{
$this
->add('name', 'text', [
'rules' => 'required|min:5'
])
->add('lyrics', 'textarea', [
'rules' => 'max:5000'
])
->add('publish', 'checkbox');
}
}
routes.php
Route::get('songs/create', [
'uses' => 'SongsController#create',
'as' => 'song.create'
]);
Route::post('songs', [
'uses' => 'SongsController#store',
'as' => 'song.store'
]);
And I do not know where is the problem because the file exist in the project folder.
Explanation of the Error
Here:
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
You're specifing the class name with a namespace relative to the current namespace:
App\Forms\SongForm::class
the full class name will be built relatively from the current namespace that is:
namespace App\Http\Controllers;
So, the class you're passing as parameter becomes:
App\Http\Controllers\App\Forms\SongForm::class
That class doesn't exists, and so you get the error
How to solve
To solve, you can specify the absolute namespace. Change this:
App\Forms\SongForm::class
to this:
\App\Forms\SongForm::class
and it should work
I have a module calls API, and i want to load config file for it. The guide says that i have to use function \Yii::configure. I use it, but it doesn't apply any new configs. And i tried to use array instead config file, the result is same
class API extends \yii\base\Module
{
public $controllerNamespace = 'api\client\controllers';
public function init()
{
parent::init();
// \Yii::configure($this, require(__DIR__ . '/config/main.php'));
\yii::configure($this, [
'components' => [
'user' => [
'class' => 'yii\web\UserTest',
'identityClass' => 'api\client\models\User',
],
]
]);
echo \yii::$app->user->className();
die();
}
}
How I can override config in my module ?
UPDATE
You have to use setComponents method of Yii::$app
Yii::$app->setComponents(
[
'errorHandler'=>[
'errorAction'=>'forum/forum/error',
'class'=>'yii\web\ErrorHandler',
],
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\modules\profile\models\User',
],
]
);
OLD ANSWER
Didn't it give you errors? Your casing are wrong and so instead of "yii" in small letters use "Yii" capitalized
class API extends \yii\base\Module
{
public $controllerNamespace = 'api\client\controllers';
public function init()
{
parent::init();
\Yii::configure($this, [
'components' => [
'user' => [
'class' => 'yii\web\UserTest',
'identityClass' => 'api\client\models\User',
],
]
]);
echo \Yii::$app->user->className();
die();
}
}
I see no reason to override the application components here. I'd use #StefanoMtangoo trick but to set the component to the Module itself instead of Yii::$app:
public function init()
{
parent::init();
$this->setComponents([
'db' => [
'class' => 'yii2tech\filedb\Connection',
'path' => '#app/builder/data',
]
]);
}
Then the tricky part is to differentiate between any app's components and your module's own components. For example if my Module had a model extending yii\db\ActiveRecord I'd override its getDB() as follow (original code here):
public static function getDb()
{
return Yii::$app->getModule('api')->get('db');
// instead of: return Yii::$app->getDb();
}
So whatever the app that is using my module has or hasn't a db component it won't matter.