Laravel 5.2 use custom Controller Trait - namespaces

I want to use my custom Trait stored in app directory into my controller. However I always get this message:
Trait 'app\MessageTrait' not found
My Controller:
namespace app\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use app\Http\Requests;
use app\User;
use app\MessageTrait;
class login extends Controller{
use MessageTrait;
public function index(Request $request){
return back();
}
}
My MessageTrait is contained within MessageTrait.php, located in app directory. Code looks like this:
My Trait:
trait MessageTrait{
public function success(){
return 'success';
}
public function error($message){
return 'error';
}
}
First I thought it may be a Namespace issue - however, User class could be found using same namespacing as my MessageTrait. Any ideas?

I solved this by adding namespace app to the top line of my Trait file. Everything works as expected now!

Related

The custom ActiveRecord class not working when i call the find() method from view?

I am overriding the find method and add the where condition globally thats working fine here is my code
class Order extends \common\components\ActiveRecord
\common\components\ActiveRecord
namespace common\components;
use Yii;
use yii\db\ActiveRecord as BaseActiveRecord;
class ActiveRecord extends BaseActiveRecord{
public static function find() {
return parent::find()
->where(['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id])
->andWhere(['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]);
}
}
The find is not working when i call the model from view like this
echo \common\models\Order::find()->where(['status'=>'0'])->count();
Since you're using where() inside the find function that you're using in ActiveRecord and then where() in this statement
echo \common\models\Order::find()->where(['status'=>'0'])->count();
the second ends up overriding the first
What you need to do is to use andWhere() in all the cases where you need the original conditions that you put in ActiveRecord to work. Try thus:
echo \common\models\Order::find()->andWhere(['status'=>'0'])->count();
This should work.
And again remember, use andWhere() in all places where the original condition needs to be applied
Use andWhere() so your default condition not get overridden :
class ActiveRecord extends BaseActiveRecord {
public static function find() {
return parent::find()
->andWhere(['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id])
->andWhere(['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]);
}
}
Try to change with onCondition()
class ActiveRecord extends BaseActiveRecord {
public static function find() {
return parent::find()
->onCondition([
['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id],
['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]
]);
}
}

Class 'App\Order' not found

I'm working with Laravel Scout for the first time, and I use the documentation form here: https://laravel.com/docs/5.4/scout#searching.
After following all the steps and adding to my route files
Route::get('warbands/search', function (Request $request) {
return App\Order::search($request->search)->get();
});
I get Class 'App\Order' not found.
Am I missing something I have to include?
Is your Order model namespaced correctly?
namespace App;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use Searchable;
...
}
If so, perhaps you need to run composer dumpautoload.
"use App\Models\Order" probably can fix it;

Laravel 5.4: attach custom service provider to a controller

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

can't load registration form

an exception appears when i am trying to customise registration form using fosuserbundle
Attempted to load class "RegistrationFormType" from namespace "PFE\UserBundle\Form\Type".
Did you forget a "use" statement for another namespace?
mu code
namespace PFE\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('nom')->add('prenom');
}
public function getName()
{
return 'pfe_user_registration';
}
}
Probably just adding a \ this way:
use \FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
Another possible solution is flushing APC. If you have set up APC sometimes it produces this kind of misterious errors.
To do that just restart your Apache, Nginx or the webserver you are using.
I hope this will help.

Call function in indexController from another controller Zend

Need to call some function from another controller (not IndexController), for example:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$someFunction = new CustomController();
$someFunction->someFunc();
}
}
But this throws an error :
Fatal error: Class 'CustomController' not found in C:\xampp\htdocs\demo.ru\application\controllers\IndexController.php on line 13
If YourController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class,
so that both controllers can access the shared functionality without having to depend on each other.
I would suggest you to follow DRY and move those functions to common library place.
to use with Namespace see
Zend Framework calling another Controller Action
hope this will sure help you.