Cakephp 3 plugin Event - cakephp

I have created a plugin called WdContactForm in cakephp3.
Inside a function of the plugin controller I need to send an email
But returns me this error:
Could not send email: Declaration of WdContactForm\Controller\AppController::beforeFilter() should be compatible with App\Controller\AppController::beforeFilter()
I have already read about adding:
use Cake\Event\Event;
I have already done it.
But I can't fix the problem
This is my controller inside plugin:
namespace WdContactForm\Controller;
use Cake\Event\Event;
use WdContactForm\Controller\AppController;
use Cake\Core\App;
use Cake\Utility\Security;
use Cake\Utility\Inflector;
use Cake\ORM\TableRegistry;
use Cake\Core\Configure;
use Cake\Mailer\Email;
class ContactFormsController extends AppController
{
public function initialize()
{
$this->loadComponent('Flash');
$this->loadComponent('Admin');
}
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
//other functions
//...
}
public function send_email()
{
$email = new Email('default');
$email->from(['test#test.it' => 'My Site'])
->to('test#test.it')
->subject('About')
->send('My message');
}
}
And this is my AppController inside the plugin:
namespace WdContactForm\Controller;
use Cake\Event\Event;
use App\Controller\AppController as BaseController;
class AppController extends BaseController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
}
}
How can I fix it?

Related

Cakephp 3 : How to insert data on db from a behavior

I've created a custom behavior on my CakePHP3 project and I would like to insert data on db from this behavior
Here is an clean exemple of my behavior, the function is well called and save of the form but the ADD Articles request doesn't work...
<?php
namespace App\Model\Behavior;
use Cake\ORM\Table;
use Cake\Event\Event;
use Cake\Core\Configure;
use Cake\I18n\I18n;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use App\Controller\AppController;
class HistorizeBehavior extends Behavior
{
public function beforeSave(Event $event, Entity $entity)
{
$this->historize($event, $entity);
}
public function historize(Event $event, Entity $entity) {
$articlesTable = TableRegistry::getTableLocator()->get('Articles');
$article = $articlesTable->newEntity();
$article->title = 'A New Article';
$article->body = 'This is the body of the article';
$articlesTable->save($article);
}
}
There is no error, no warning...but the data isn't saved..
Any idea why ?
Thanks

React login screen with laravel [duplicate]

In my LoginController under Auth, I have used the following codes:
namespace App\Http\Controllers\Auth;
use App\Model\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Hash;
use Auth;
use DB;
use App\Model\UserAdmin;
class LoginController extends Controller {
use AuthenticatesUsers;
public function __construct() {
$this->middleware('guest')->except('logout');
}
public function doLogin(Request $request) {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'status' => '1',
);
if (Auth::guard('admin')->attempt($userdata)) {
return Redirect::intended('/administrator/dashboard')->with('successMessage', 'You have successfully logged in.');
}
}
}
And in UserAdmin (model) under app/Model is as follows:
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Config;
class UserAdmin extends Authenticatable {
protected $table = 'adminusers';
public $timestamps = false;
protected $fillable = ['firstName', 'lastName', 'email', 'company', 'website'];
public function __construct() {
parent::__construct(); // Don't forget this, you'll never know what's being done in the constructor of the parent class you extended
}
}
After submitting the login details, it shows me the error:
Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Model\UserAdmin given, called in /var/www/html/XXXXXX/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 379
I suppose that you required to add implements \Illuminate\Contracts\Auth\Authenticatable to your UserAdmin model class definition.
class UserAdmin extends Model implements
\Illuminate\Contracts\Auth\Authenticatable
You must use Authenticatable in User model
for example:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
//your code
}
You must declared use AuthenticableTrait for Authenticatable interface.
For example :
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;
class Company extends Model implements Authenticatable
{
use AuthenticableTrait;
Try and run 'composer dump-autoload' to check for "ambiguous User class resolution". It is likely you have two classes Defined as User Class.
use Illuminate\Foundation\Auth\AuthenticatesUsers;
Then in your model class, extends AuthenticatesUsers instead of Model.
You must extends Authenticatable class and implements JWTSubject in User model
For example :
class User extends Authenticatable implements JWTSubject {
Go to your Model and instead of extending Model, extend User
<?php
namespace App;
class Staff extends \Illuminate\Foundation\Auth\User
{
}

Respond as XML not working since cakePHP 3.1

I need to render an XML+XSL template in my application, and it used to work with cakePHP 3.0. I have made the switch to 3.1 recently and it has stopped working. The problem is that I was having a formatted view of my XML, while now I just get a plain string.
The migration guide says something about some changes in the RequestHandlerComponent, but nothing helpful (or maybe it's just me and I don't get the point :)).
This is my controller (it is exactly as it was with Cake3.0):
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Utility\Xml;
use Cake\Event\Event;
use Cake\Routing\Router;
use Cake\ORM\TableRegistry;
use Cake\Filesystem\Folder;
use Cake\Filesystem\File;
use Cake\Network\Email\Email;
use Cake\Core\Configure;
use Cake\I18n\Time;
/**
* Invoices Controller
*
* #property App\Model\Table\InvoicesTable $Invoices
*/
class InvoicesController extends AppController
{
public $components = [
'Browser',
'Reorder11'
];
public $helpers = [
'Multiple'
];
public $paginate = [];
public function initialize()
{
parent::initialize();
$this->loadComponent('Paginator');
$this->loadComponent('RequestHandler');
}
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow(['demo']);
}
/*
* ... several other functions ...
*/
public function viewxml($id = null)
{
$this->viewBuilder()->layout('xml');
$invoice = $this->Invoices->myInvoice($id, $this->Auth->user('id'));
$this->RequestHandler->respondAs('xml');
$this->set('invoice', $invoice);
}
}
The xml.ctp layout, which is really simple
echo $this->fetch('content');
and the viewxml.ctp template just echoes the xml as a string.
How can I obtain the formatted XML+XSL again?
Try add: $this->response->header(['Content-type' => 'application/xml']);
I had the same error but my output was pdf
working 3.0.14 using this code:
$this->RequestHandler->respondAs("pdf");
$this->layout = 'pdf/default';
$this->view = 'pdf/report1_pdf';
for 3.1.x (this works if u save the file and open later, if you try to open it directly on browser its print the plain file content as a txt/html):
$this->viewBuilder()->layout('pdf/default');
$this->viewBuilder()->template('pdf/report1_pdf');
$this->RequestHandler->respondAs('pdf');
$this->response->header(['Content-type' => 'application/pdf']);

Cakephp3.0 I am calling Postcategories controller into Appcontroller and it is error " Call to undefined method Cake\Core\App::import() "

My Code tries to fetch all Main categories of the posts into Appcontroller to show on the homepage:
namespace App\Controller;
use Cake\Core\App;
use Cake\Controller\Controller;
class AppController extends Controller
{
public $helpers = ['Html', 'Form', 'Session','Time','Tree'];
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->maincategories();
}
function maincategories(){
App::import('Controller','Postcategories');
$postcates = new PostcategoriesController;
$postcates = $postcategory->find('threaded');
}
}
Your maincategories() method is wrong. You need the model, not the controller to retrieve the data from. You need to use TableRegistry::get('Postcategories') to get the Postcategories model and then call the find on that:-
public function maincategories()
{
$Postcategories = TableRegistry::get('Postcategories');
$this->set('postcategories', $Postcategories->find('threaded'));
}
$this->set() is setting the categories as a view variable ($postcategories). You will need to make sure you include use Cake\ORM\TableRegistry; at the top of your AppController file.
Make sure you've fully read the docs on retrieving data.

How to implement a default view in CakePHP?

In CakePHP each method of a Controller has its own View and the view template file is the name of the method.
class DataController extends AppController
{
public function one()
{
// will render one.ctp
}
public function two()
{
// will render two.ctp
}
}
Accourding to the API documentation there is a $view property of the Controller that specifies the view to render. So I should have the ability to specify a default view file, say all.ctp, for all methods of a controller
class DataController extends AppController
{
public $view = 'all';
public function one()
{
// should render all.ctp
}
public function two()
{
// should render all.ctp
}
}
However this does not work and CakePHP ignores the $view property and continues to look for the template file of the same name as the method.
Is there a way to have a default view without having to insert $this->render('all'); in each of the Controller's methods?
The value is going to be overridden in Controller::setRequest() which is being called in the controllers class constructor.
You could use your controllers beforeFilter() callback instead to set the value:
public function beforeFilter()
{
parent::beforeFilter();
$this->view = 'all';
}

Resources