Pimcore 4 extending Document/Page - php

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!

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

Adding Facades before namespace in Laravel , How it works?

Okay there are questions about the same topic before but they don't help to fully understand this topic
SO SuggestionFirst
SO Suggestion Second
All the code is just to illustrate the situation, So this is the structure
A helper function which does something
namespace App\Helpers;
class Pets{
public function limit($string,$limit,$start = 0){
return substr($string,$start,$limit);
}
}
Now in order to use this helper, since it's a class so i need to create an object like this
CODE SAMPLE FIRST
namespace App\Objects;
use App\Helpers\Pets;
class User{
public function getShortUserName(){
$name = auth()->user()->first_name.' '.auth()->user()->last_name;
$pet = new Pets;
return $pet->limit($name,10);
}
}
But somewhere I got to know that if you add Facades before your namespace, you can call the function statically even if they are non static function like this
CODE SAMPLE SECOND
namespace App\Objects;
use Facades\App\Helpers\Pets;
class User{
public function getShortUserName(){
$name = auth()->user()->first_name.' '.auth()->user()->last_name;
return Pets::limit($name,10);
}
}
Now what I want to know is I have 2 sample codes with namespace as follows
use App\Helpers\Pets;
use Facades\App\Helpers\Pets;
By adding the Facades I can call the function statically but how, that's not a valida namespace in my app
What laravel doing behind the scene, I am so confused
Thank you for your time ;)
What you are describing is Laravels Real-Time Facades.
You can find documentation of the functionality here:
https://laravel.com/docs/6.x/facades#real-time-facades
I will not enter too much in details but this is a simple explanation of what's behind the scenes when you use facades in laravel.
Let's suppose you define a custom class with some public methods:
namespace Test;
class Foo
{
public function test()
{
return 'test';
}
}
Then you have to define a facade for this class:
namespace Test1;
class BarFacade
{
// In laravel this is called in the Facade abstract class but it is actually implemented
// by all the facades you add across the application
public static function getFacadeAccessor()
{
// In laravel you can also return a string which means that the object
// will be retrieved from the container.
return new \Test\Foo();
}
// In laravel this method is defined in the Facade abstract class
public static function __callStatic($method, $args)
{
$object = self::getFacadeAccessor();
return call_user_func_array([$object, $method], $args);
}
}
Then, you have to define the alias in the $aliases array of the config.app file. These aliases are parsed by laravel and registered using the php built-in function class_alias (see Illuminate/Foundation/AliasLoader.php)
class_alias('Test\Foo', 'BarFacade', true);
// You can also create an alias for the facade itself
class_alias('Test1\BarFacade', 'FooBar', true);
Then you can simply call the facades:\
var_dump(BarFacade::test());
var_dump(\Test1\BarFacade::test());
var_dump(\FooBar::test());
The results would obviously be:
string(4) "test"
string(4) "test"
string(4) "test"

FuelPHP simple class Not Found Error

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

Gas ORM - class not found

I was trying out Gas ORM, and have managed to auto-generate my models and now need to test them. However, I cannot seem to access the newly generated model.
I have the library autoloaded, and the config set up as:
config/gas.php
$config['models_path'] = array('GasModel' => APPPATH.'gas');
gas/useraccounts.php
<?php namespace GasModel;
/* This basic model has been auto-generated by the Gas ORM */
use \Gas\Core;
use \Gas\ORM;
class UserAccounts extends ORM {
public $primary_key = 'id';
function _init()
{
self::$fields = array(
'id' => ORM::field('auto[11]'),
...
);
}
}
controller/user.php
public function test() {
GasModel\UserAccounts::all()
}
Trying to access it however throws a fatal error:
PHP Fatal error: Class 'GasModel\UserAccounts' not found in applications/controllers/user.php on line 28
Can anyone help me in solving this issue?
Try add use of your model namespace or try add \ before GasModel
When I used back the same namespace as the example in http://gasorm-doc.taufanaditya.com/configuration.html, which is Model, it started working mysteriously. I would have preferred to use my custom namespace though..

Categories