in my Laravel 9 project, the model does not retrieve users' data from the table, probably because it extends authenticatable instead of Model. and when I changed that, the authentication did not work.
Here is my model:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
}
?>
I tried the controller and view code on another model and it worked.
what would the problem be?
I was trying to display records of users table but it keeps giving me the error Undefined variable $users.
I'm using Route::resource
Related
Looking at the laravel docs for controllers, they have an example where they authorize actions via a policy inside a Controller class:
$this->authorize('action',$model);
Yet checking the laravel doc api, I can see that the base controller class has no authorize method. How does this work?
If I take a look at a standard controller that is scaffolded by Laravel, you'll see that it extends a "base" controller in your app/Http/Controllers folder;
use App\Http\Controllers\Controller;
class YourController extends Controller
{
//...
If you go and take a look at this "base" Controller class:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
You can see that this is using the AuthorizesRequests trait.
If you go and take a look in the AuthorizesRequests trait you'll see these authorisation methods.
One of which is:
public function authorize($ability, $arguments = [])
{
[$ability, $arguments] = $this->parseAbilityAndArguments($ability, $arguments);
return app(Gate::class)->authorize($ability, $arguments);
}
This is how the authorize method works and is provided to your controllers.
I want to override default notifications for password reset and email verification in laravel 6 to use queue in a simplest way as possible. So I add methods in User.php model:
use App\Notifications\ResetPasswordNotification;
use App\Notifications\EmailVerificationNotification;
...
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
public function sendEmailVerificationNotification()
{
$this->notify(new EmailVerificationNotification);
}
and create new notifications
ResetPasswordNotification
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Notifications\ResetPassword;
class ResetPasswordNotification extends ResetPassword implements ShouldQueue
{
use Queueable;
}
EmailVerificationNotification
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Notifications\VerifyEmail;
class EmailVerificationNotification extends VerifyEmail implements ShouldQueue
{
use Queueable;
}
Now email verification is sending queued, but in url as a host name is generating http://localhost/... In default notification it is generated correctly, the same one like a domain name in browser (without changing it in .env file).
The second problem is with password reset notification, which is not sending at all. It gives me an error
Trying to get property 'view' of non-object at vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php:92
and I don't understand why it is happening and don't working as expected.
Searching the problem I even found this (question) where fakemeta mention about it that should work.
I figured it out. First, when using queue, in my case I am running artisan queue:work with supervisor daemon, jobs are running under the console, so there is no SERVER['HTTP_HOST'] var available which mean than this value must be read from .env file. Second, when You change code overriding methods like I did, you must restart this queue:work to re-read changes. So those were my main problems.
I am new to YII framework. My whole code is correct but undefined namespace error is present. Please set the error.
model
<?php
namespace app\models;
use yii\base\Model;
view
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
controller
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\UserForm;
error
Undefined namespace filters
Undefined namespace web
Undefined namespace models
I guess you are using advanced applocation of Yii2. If so, it is better to use namespace frontend\models; or namespace backend\models; relative where those file is located
I am setting up a multi-module application, so far I have it setup like this example http://docs.phalconphp.com/en/latest/reference/applications.html.
But I was wandering if its possible to have shared base controller that both the backend and frontend controllers extend from. This is so I can have a single ACL in the base controller. How would I set that up?
According to the docs I can create a controllerbase anywhere and then just require this file directly in the bootstrap file or cause to be loaded using any autoloader. So I created a folder called apps/shared/controllers/ControllerBase.php and required this file directly in the bootstrap file but this does not work.
If I try to load a controller like so:
class AdminController extends ControllerBase
{
public function indexAction()
{
echo "<h1>Hello admin!</h1>";
}
}
I get an error ...Backend\Controllers\ControllerBase' not found in......
So how do I cause to be loaded using any autoloader as per the docs? Do I need to register it as its own namespace or something?
You not using the full namespace path for your base controller so the autoloader attempts to find it under in the same namespace of the child class.
Try something like this:
namespace MyApp\Backend\Controllers;
use MyApp\Shared\Controllers\ControllerBase;
class AdminController extends ControllerBase
{
public function indexAction()
{
echo "<h1>Hello admin!</h1>";
}
}
This answer consider that you have applied the PSR-0 and PSR-4 properly.
I started writing my first Zend Framework 2.0 (beta 1) PHP application using php 5.3.
I actually created a skeleton project and module based on the following url: http://packages.zendframework.com/docs/latest/manual/en/zend.mvc.quick-start.html
I want to add forms to the module I created. the my question is how do I configure the module to know where to fetch the forms in?
my module name is called LoginModule and I created a new form called LoginForm (that extends Zend_Form) and I placed it in my_proj/module/LoginModule/src/LoginModule/forms
how do I configure that module to know where to fetch the form class from ?
thanks
found the answer at http://akrabat.com/getting-started-with-zend-framework-2/
Everything has changed... (got better) in zend framework 2.
I created a directory called 'Form' in the src directory of my module.
inside I create the class that extends Form (not Zend_Form)
example from the tutorial above:
<?php
namespace Album\Form;
use Zend\Form\Form,
Zend\Form\Element;
class AlbumForm extends Form
{
public function init()
{
$this->setName('album');
$id = new Element\Hidden('id');
$id->addFilter('Int');
$artist = new Element\Text('artist');
$artist->setLabel('Artist')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$title = new Element\Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Element\Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id, $artist, $title, $submit));
}
}
i really recommend reading the tutorial for all zend framework 2 beginner :)
thanks!