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
Related
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.
I am facing an issue, I hope you guys are able to help me.
Basically I need to check a class method and get the return type class name.
Since PHP 7.0 is no longer possible cast (string)ReflectionMethod::getReturnType() to string, we get an error.
However the getReturnType() return an instance of ReflectionNamedType, with Laravel helper dd() I got the following:
ReflectionNamedType {
name: "Illuminate\Database\Eloquent\Relations\BelongsTo"
allowsNull: false
isBuiltin: false
}
The question is, since I cannot cast... how I can be able to get the name?
Thank you!
ReflectionMethod is actually the wrong class to be using, in this case you want ReflectionClass.
Using Tinker I was able to get the name correctly with the following simple test:
// User.php
class User extends Model
{
public function foos()
{
return $this->belongsToMany('App\Models\Foo');
}
}
// Foo.php
class Foo extends Model
{
public function users()
{
return $this->belongsToMany('App\Models\User');
}
}
$u = new User();
$r = new \ReflectionClass($u->foos());
echo $r->getName(); // Outputs "Illuminate\Database\Eloquent\Relations\BelongsToMany"
To solve that I use the method ReflectionNamedType::getName().
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!
I'm trying to write some unit tests for a brand new mini app. I usually write functional tests so this is me branching out to try and do it properly with mocking and stubs and all those things that make it just about the code.
The model looks like this :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class myModel extends Model
{
public static function getUser()
{
$user = \Auth::user();
return $user->adldapUser->cn[0];
}
}
And the test :
class MyModelTest extends TestCase
{
public function testGetUser()
{
$mockResult = new StdClass();
$mockResult->adldapUser = new stdClass();
$mockResult->adldapUser->cn=array("test");
$auth = $this->getMock('Auth');
$auth
->method('user')
->will($this->returnValue($mockResult));
$this->assertEquals('test',\App\MyModel::getUser());
}
}
But when I run the unit test I get the following error message :
There was 1 error:
1) MyModelTest::testGetUser ErrorException: Trying to get property of
non-object
/home/aidan/web/vagrant-web-dev/src/apps/orcid/app/MyModel.php:61
/home/aidan/web/vagrant-web-dev/src/apps/orcid/tests/MyModelTest.php:18
and if I post out $user it's NULL.
What am I doing wrong here?
Here I go again answering my own question half an hour after asking it.
For prosperity's sake I'll leave it here.
The answer was in this post here : https://stackoverflow.com/a/17602763/808124
So I replaced the $this->getMock with
Auth::shouldReceive('user')->once()->andreturn($mockResult);
Working
I use it this way:
$user -factory(User:class)->create();
\Auth::shouldReceive('guard')->andReturnSelf()
->shouldReceive('user')->andReturn($user)
->shouldReceive('check')->andReturn(true);
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();