I have tried many times this error in different ways also tried one sollution from: CakePHP: Call to a member function setFlash() on a non-object url ... But this solution is also not working in my project.
Cant recognize whats the error. !!!
Here is my User.php model
class User extends AppModel {
public $helpers = array('Html','Form');
public $components = array('Email','Flash','Session');
public function send_mail($useremail){
$Email = new CakeEmail('gmail');
$Email->emailFormat('html')
->from('abc#xyz.com')
->to($useremail)
->subject('User subject');
if($Email->send("123")){
$this->Flash->success(__('Mail Sent')); // **this line cause error**
}else{
$this->Session->setFlash('Problem during sending email');
}
}
}
As the documentation suggest, FlashComponent provides two ways to set flash messages: its __call()magic method and its
set() method. To furnish your application with verbosity, FlashComponent’s __call() magic method allows you use a method name that maps to an element located under the src/Template/Element/Flash directory. By convention, camelcased methods will map to the lowercased and underscored element name:
// Uses src/Template/Element/Flash/success.ctp
$this->Flash->success('This was successful');
// Uses src/Template/Element/Flash/great_success.ctp
$this->Flash->greatSuccess('This was greatly successful');
Alternatively, to set a plain-text message without rendering an element, you can use the set() method:
$this->Flash->set('This is a message');
And hence, you need to change it by typing
$this->Flash->success('Mail sent');
Related
I have this basic app that i am working on in Laravel. A user asks a query and other users can comment on it.
I have this relationship built in the Comment model-
class Comment extends Model
{
public function query()
{
return $this->belongsTo(Query::class);
}
}
When i run php artisan tinker and create a new instance like so-
$comment = new App\Comment
I get an error-
Cannot make static method Illuminate\Database\Eloquent\Model::query() non static in class App\Comment
The problem i figured was with the name of the function 'query'. Because if i change the name to anything else, it works. I don't get any error.
I found out that there is a function in lluminate\Database\Eloquent\Model with the name 'query' that has this code in it-
public static function query()
{
return (new static)->newQuery();
}
So, am I not allowed to use the word 'query' to name the function in my model?
Method query() is used in Model. So, only rename your method to avoid bad behavior.
I have this following Controllers on my file
class MyController1 extends BaseController{
public function notify($id,$message){
//Sending a push notification
//Google Cloud Messaging Here
//****//
}
}
How do i write the route to pass a value to id and message using GET?
Route::get("/sendMessage" , MyController1#notify);
the url should be something like
https://mysite.com/sendMessage?id=1&message=Hello
Also I need to call the notify method from other controllers like this. .
class MyController2 extends BaseController{
public function something(){
$con = new MyController2();
$con->notify($id,$message);
}
}
What should I put to the notify the model?
This is the code you need to create GET parameters in your URL:
Route::get('sendMessage/{id}/{msg}'
More information here:
http://laravel.com/docs/routing#route-parameters
Greetings
Laravel URL's work slightly differently to how you want it.
In Laravel as standard your URL for the above example will be
https://mysite.com/sendMessage/1/Hello
and your route would be
Route::get("/sendMessage/{id}/{message}" , MyController1#notify);
the text in the braces {} will be the name of the parameter passed to the controller function
eg. https://mysite.com/sendMessage/1/Hello would call MyController->notify(1,'Hello');
Why do I get this error?
Fatal Error
Error: Call to a member function find() on a non-object
File: /home/mycake/public_html/app/Controller/TasksController.php
Line: 7
I think it has something to do with using CAKE 2.0 but I think the code in my controller might be CAKE 1.3? And I have done a bit of research but I don't know how to change the code to be in CAKE 2.0. Can anyone help?
This is the TasksController.php page
<?php
class TasksController extends AppController {
public $name = 'tasks';
public function index() {
//THIS IS THE LINE 7
$tasks = $this->Task->find('all');
$this->set('tasks', $tasks);
}
}
If you need any more info please ask because I'm not sure how else to make it more relevant to get an answer :)
If you controller is called TasksController then it will try and instantiate the Task model automatically. You don’t need to manually specify it. The reason CakePHP is throwing an error is because you’ve pluralised the name (models are singular, so Task not Tasks) and are also camel-cased, meaning they start with an uppercase letter (Task not task).
CakePHP does auto instantiation of corresponding Model if the particular controller's property $uses is not set, so this means you might accidentally setting the property $uses of your AppController to either null or array().
So, check that if you have $uses in your AppController set to empty, or simply, in your TasksController, overwrite the value with public $uses = array('Task');.
You can try the index like this:
class TasksController extends AppController{
public function index(){
$this->set('tasks', $this->Task->find('all'));
}
If you get the Error: Call to a member function find() on a non-object, You can put:
$this->loadModel('Task');
This is my custom validation function. It uses geocoding from a Google Maps CodeIgniter library to check if a location exists.
public function address_check($str)
{
$this->load->library('GMap');
$this->gmap->GoogleMapAPI();
// this method checks the cache and returns the cached response if available
$geocodes = $this->gmap->getGeoCode("{$str}, United States");
$this->form_validation->set_message('address_check', 'The %s field contains an invalid address');
if (empty($geocodes))
{
return FALSE;
}
else
{
return TRUE;
}
}
If I place the function above inside my Controller along with the following rule, it works perfectly well.
$this->load->library('form_validation');
$this->form_validation->set_rules('location', 'Location', 'callback_address_check');
Now I simply want to move it out of my Controller. So I'm trying to extend my CodeIgniter Form Validation library as per this SO answer and the CI documentation.
I created a file here: /codeigniter/application/libraries/MY_Form_validation.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function address_check($str)
{
$this->load->library('GMap');
$this->gmap->GoogleMapAPI();
// this method checks the cache and returns the cached response if available
$geocodes = $this->gmap->getGeoCode("{$str}, United States");
$this->form_validation->set_message('address_check', 'The %s field contains an invalid address');
if (empty($geocodes))
{
return FALSE;
}
else
{
return TRUE;
}
}
}
And from within my controller, I am setting the rule like this...
$this->load->library('form_validation');
$this->form_validation->set_rules('location', 'Location', 'address_check');
The first problem I found and solved myself was that nothing was happening because that SO answer incorrectly specified the file name to be My_Form_validation.php where it should have been MY_Form_validation.php
Now that the function is being called, the new problem is that I am getting the following error:
Message: Undefined property: MY_Form_validation::$load
Filename: libraries/MY_Form_validation.php
Line Number: 12
This is line 12:
$this->load->library('GMap');
I can't access a library from within a library? What's the proper way to fix this? I'd prefer not to auto-load the GMap library since I won't be using it all the time. Any other problems within my method?
Use this:
$CI =& get_instance();
$CI->load->library('GMap');
And then use it like:
$CI->gmap->GoogleMapAPI();
You have to do it that way, because Form Validation is not like CI Model or Controller class, it's only library.
Inside of your library, you can extend other models, libraries, configs, helpers, etc... by first loading the CI class. For example, in your constructor you can accomplish this through the following:
public function __construct()
{
parent::__construct();
$this->ci =& get_instance();
}
Once you have loaded the CI class, you can load any other classes that may need loading.
Examples:
$this->ci->load->library("session");
$this->ci->load->model("my_model");
$this->ci->load->helper("file");
Or in your case:
$this->ci->load->library("GMap");
You can then call functions from the class in a similar fashion throughout your class:
$this->ci->gmap->GoogleMapAPI();
$this->ci->gmap->getGeoCode("{$str}, United States");
I am trying to set a validation message for a set_rule....is this possible?
I tried this
$this->form_validation->set_rules('payment_amount', 'Payment Amount', 'regex_match[/^\d{0,4}(\.\d{1,2})?$/]');
$this->form_validation->set_message('payment_amount', 'This is a test');
and my message did not change.
Any ideas?
You can set a custom validation error message by creating a function for the rule.
This code is untested.
public function payment_amount($str)
{
if (preg_match('[/^\d{0,4}(\.\d{1,2})?$/]', $str)
{
$this->form_validation->set_message('payment_amount', 'The %s field has an error');
return FALSE;
}
else
{
return TRUE;
}
}
Us CI's callback as follows:
$this->form_validation->set_rules('payment_amount', 'Payment Amount', 'callback_payment_amount')
Create MY_Form_validation.php & put your code into.
Use the documentation (http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html)
Extending Native Libraries
If all you need to do is add some functionality to an existing library - perhaps add a function or two - then it's overkill to replace the entire library with your version. In this case it's better to simply extend the class. Extending a class is nearly identical to replacing a class with a couple exceptions:
The class declaration must extend the parent class.
Your new class name and filename must be prefixed with MY_ (this item is configurable. See below.).
For example, to extend the native Email class you'll create a file named application/libraries/MY_Email.php, and declare your class with:
class MY_Email extends CI_Email {
}
Note: If you need to use a constructor in your class make sure you extend the parent constructor:
class MY_Email extends CI_Email {
public function __construct()
{
parent::__construct();
}
}