FuelPHP simple class Not Found Error - php

This is my first deployment of FuelPHP, though I am a long time user of CodeIgniter.
I am getting the following error when I load the page:
ErrorException [ Fatal Error ]:
Class 'Model\Model_UPS' not found
/classes/controller/ups.php
<?php
use \Model\Model_UPS;
class Controller_UPS extends Controller {
public function action_index() {
$view = View::forge('json');
$view->title = Model_UPS::get_load();
return $view;
}
}
?>
/classes/model/model_ups.php or ups.php
<?php
namespace Model;
class Model_UPS extends \Model {
public static function get_load() {
return "This is the load!";
}
}
?>
/views/json.php
<?=$title;?>
The error page highlights the $view->title = Model_UPS::get_load(); line of ups.php. I have tried just about every configuration of use, namespace, model filename, and model class name that I can think of. I can't seem to find a super simple MVC example to use as a guide. I've tried to duplicate the FuelPHP Docs as best as I can, but have failed. Can anyone find anything wrong with this?

Rename file: model/model_ups.php to model/ups.php
Rename class: Model_UPS to UPS
Change: use \Model\Model_UPS; to use \Model\UPS;
Change: Model_UPS::get_load(); to UPS::get_load();

Related

Phalcon - How to call a model in another controller? "Error: Class not found"

How to call a model in another controller?
I explain myself, I created a controller and I try to call another model in this controller, but I have the error "Error: Class not found".
You can see the code of my controller "Adserver" trying to call the model "Zone"
<?php
declare(strict_types=1);
use Phalcon\Mvc\Model\Zone as Zone;
class AdserveurController extends ControllerBase
{
public function indexAction()
{
$id_zone= 1;
$zone = Zone::findFirstByid_zone($id_zone);
if(!zone){
$this->flashSession->error('erreur id');
return $this->response->redirect();
}
print_r ($id_zone);
$zone = Zone::findFirst($hauteur);
if(!zone){
$this->fashSession->error('erreur hauteur');
return $this->response->redirect();
}
$zone = Zone::findFirst($largeur);
if(!zone){
$this->fashSession->error('erreur hauteur');
return $this->response->redirect();
}
}
}
On top of my controller, I tried the "use Phalcon\Mvc\Model" and the error persists.
My phalco version is 4.0
Could someone help me on how to call two models in a separate controller?
Thanks.
I call multiple models from withing various controllers all the time and don't have to declare "use Phalcon\Mvc\Model\Zone as Zone;" at the top.
How is your Zone model file defined?
if other_model has relation with Zone (defined in model) then :
Zone->other_model->field;
else in head of file
"use Phalcon\Mvc\Model*other_model*;
Try to define the namespace in your model, like:
// app/models/Zone.php
<?php
namespace MyApp\Models;
use Phalcon\Mvc\Model;
class Zone extends Model
{
public function initialize()
{
$this->setSource('zone');
}
}
Then in your loader:
<?php
use Phalcon\Loader;
$loader->registerNamespaces(
[
'MyApp\Models' => 'app/models'
]
);
$loader->register();
Then in your controller:
<?php
use MyApp\Models\Zone;
Your current indexAction should work, but beware of what I commented to you regarding fields with underscores:
$zone = Zone::findFirstByid_zone($id_zone); //won't work
$zone = Zone::findFirstByIdZone($id_zone); //works
Since you commented that you are a beginner, I would recommend you to review the MVC examples for Phalcon projects: https://github.com/phalcon/mvc So you get acquainted with namespaces in your app.
Also, if you are working on a large application, it's better that you register namespaces than directories for performance reasons, as detailed in the Docs: https://docs.phalcon.io/4.0/en/loader
Ps. Reviewing indexAction there are some parameters that are not defined, like $hauteur and $largeur. Since $zone still is not working, these issues are still not visible --but they'll show up after zone is working.

CodeIgniter 4 Class not Found, problem with Namespaces

I tried to create a Helper in CodeIgniter 4 but I can't get it loaded.
I tried the following, but to no effort. I'm new to CodeIgniter 4 and namespaces so I guess I'm doing something wrong but I can't find what. What could be wrong?
When running I get an Error:
Error
Class 'App\Helpers\php2jquery' not found
Thanks for any help.
Edward
This is the controller:
<?php namespace App\Controllers;
use App\Helpers\php2jquery;
class Test extends BaseController
{
public function index()
{
$param = “”; //Doesn’t matter here ;
$jqueryparam = New php2jquery();
$data[‘jqueryobject’] = $jqueryparam->php_array_to_jquery_param($param, 4, "new FWDRAP", "FWDRAPUtils.onReady(function(){" );
$data['base'] = config('App')->baseURL;
return view('test_message',$data);
}
}
and this is the Helper in App/Helpers/php2jquery
(I also tried php2jquery_helper)
<?php
class php2jquery
{
function php_array_to_jquery_param($param,$indent=0, $object="", $wrapfunction=""){
Return (“this is a test”); //Dummy
}
}
in App/Helpers/php2jquery set namespace
and try in controller wite "app\Helpers" with small letter

Doctrine Entity from other Bundle in Symfony Controller

I try to use the Entity of another Bundle in my Symfony Controller:
use Acme\TestBundle\Entity\Neighbour;
use AppBundle\Entity\Home;
class TestController extends Controller {
public function testAction(Home $home, Neighbour $neighbour) {
//
}
}
but this throws an 404 Error:
Acme\TestBundle\Entity\Neighbour object not found
this is different to a real not existing object like NeighbourX, there it throws an 500 error:
Acme\TestBundle\Entity\Neighbour does not exist
The object exists, and it should work, because this works:
use Acme\TestBundle\Entity\Neighbour;
use AppBundle\Entity\Home;
class TestController extends Controller {
public function testAction(Home $home) {
$thread = new ForumThread();
}
}
Ok I already found the answer myself. I had to specify the route variable:
/home/{id}/neighbour/{nid} #before
/home/{id}/neighbour/{neighbour} #after
But I don't fully understand it. Why doesn't {id} has to be {home}? Is {id} just the first parameter-id by default?
And why is the error message that misleading..

Pimcore 4 extending Document/Page

Again I am having trouble getting the class mappings to work in Pimcore 4. This time I want to extend the document page class. This used to work without problems in older versions, but now I cannot get it working.
I copied this example in classmap.php from classmap.example.php:
website/config/classmap.php:
return [
"Document\\Page" => "Website\\Model\\Document\\Page",
]
website/models/Website/Model/Document/Page.php:
namespace Website\Model;
use Pimcore\Model\Document;
class Page extends Document\Page {
public function getPublicPath() {
return $this->getFullPath();
}
}
The expected result is that I can call getPublicPath() on every document\page object. But this is not working. Instead I get the following error:
Call to undefined method getPublicPath in class Pimcore\Model\Document\Page
How do I get this working?
Your namespace declaration is wrong. It should be:
namespace Website\Model\Document;
So the whole class looks like this:
<?php
namespace Website\Model\Document;
use Pimcore\Model\Document;
class Page extends Document\Page {
public function getPublicPath() {
return $this->getFullPath();
}
}
Don't forget to clear the cache after updating your code!

Yii Custom Validation - Class Validate contains 1 abstract method

I have some validation methods that I need to use for multiple models. For example validation of phone numbers can be shared across multiple models.
I understand from http://www.yiiframework.com/wiki/56/ I can create an extension that can be used by multiple models, for example:
array('phone', 'ext.Validate.Validate'),
I have modified this line a few times and can confirm it is hitting the right file.
With the following in Validate.php
class Validate
I get the error Call to undefined method Validate::applyTo(), therefore I have changed it to
class Validate extends CValidator
As suggested by the link above, however I now get the error:
Class Validate contains 1 abstract method
Here is the file as it stands:
<?php
class Validate extends CValidator
{
public function phone($phone)
{
if(!ctype_digit($phone))
{
$this->addError($phone, Yii::t('flash','flash.not_authorised',array('{attribute}'=>$phone)).' '.ucfirst(str_replace('_', ' ', $phone)).' field');
}
else
{
return true;
}
}
}
Can someone point me in the right direction as to how I can have a shared validation between multiple methods using the above.
Ok I managed to solve this.
I found the following after a few hours of searching http://www.yiiframework.com/wiki/56/
The main issue is tat you need the following method in your class, when extending CValidator:
protected function validateAttribute($object,$attribute)
{
}
Here is the model code (yii/protected/extension/Validate/Validate.php)
array('phone', 'ext.Validate.Validate'),
Here is the class code:
<?php
class Validate extends CValidator
{
protected function validateAttribute($object,$attribute)
{
self::{$attribute."Validation"}($object,$attribute);
}
protected function phoneValidation($object,$attribute)
{
if(!empty($object->$attribute))
{
if(!ctype_digit($object->$attribute))
{
$this->addError($object,$attribute,Yii::t('app','validation.telephon_failed',array('{attribute}'=>ucwords($attribute))));
}
}
}
}

Categories