-
music_ed
Newbie
-
Posts: 5
Threads: 2
Joined: Nov 2020
Reputation:
0
I'm new to Codeigniter 4 and am struggling with namespaces. I tried to create a helper php2jquery.php in App/Helpers I cannot load it the way I try it. This is wat I have now.
PHP Code: <?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); }
//--------------------------------------------------------------------
}
———— File: App/Helpers/php2jquery.php (Also tried App/Helpers/php2jquery_helper.php __________ <?php class php2jquery { function php_array_to_jquery_param($param,$indent=0, $object="", $wrapfunction=""){ Return (“this is a test”); } }
Now, when I run it I get "Error: Class App\Helpers\php2jquery not found" I tried adding _helper tot the name and call it with and without it, but no luck. Can somebody tell me what is wrong? Thank you
-
InsiteFX
Super Moderator
-
Posts: 6,575
Threads: 331
Joined: Oct 2014
Reputation:
240
Because you are doing the helper wrong see below how to do it the helper is loaded in the BaseController.
PHP Code: <?php namespace App\Controllers;
class Test extends BaseController {
public function index() { $param = ""; //Doesn’t matter here ; $data['jqueryobject'] = phpArrayToJqueryPparam($param, 4, "new FWDRAP", "FWDRAPUtils.onReady(function(){" ); $data['base'] = config('App')->baseURL; return view('test_message', $data); }
//--------------------------------------------------------------------
}
———— File: app/Helpers/php2jquery_helper.php (Also tried App/Helpers/php2jquery_helper.php __________ <?php
if ( ! function_exists('phpArrayToJqueryParam')) { function phpArrayToJqueryParam($param, $indent = 0, $object = "", $wrapfunction = "") { $tmp = 'this is a test'; return $tmp; } }
// End of app/Helpers/php2jquery_helper.php
// ------------------------------------------------------------------------
// Start app/Controllers/BaseContoller.php
<?php
namespace App\Controllers;
use CodeIgniter\Controller; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\Services; use Psr\Log\LoggerInterface;
/** * Class BaseController * * BaseController provides a convenient place for loading components * and performing functions that are needed by all your controllers. * Extend this class in any new controllers: * class Home extends BaseController * * For security be sure to declare any new methods as protected or private. */
/** * Class BaseController * * @package App\Controllers */ class BaseController extends Controller { /** * An array of helpers to be loaded automatically upon * class instantiation. These helpers will be available * to all other controllers that extend BaseController. * * @var array */ protected $helpers = [ 'php2jquery', ];
/** * @var \CodeIgniter\Session\Session */ protected $session;
/** * initController () * ------------------------------------------------------------------- * * @param RequestInterface $request * @param ResponseInterface $response * @param LoggerInterface $logger */ public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { // Do Not Edit This Line parent::initController($request, $response, $logger);
//-------------------------------------------------------------------- // Preload any models, libraries, etc, here. //-------------------------------------------------------------------- // E.g.: // $this->session = \Config\Services::session();
// Ensure that the session is started and running if (session_status() == PHP_SESSION_NONE) { $this->session = Services::session(); }
}
} // -------------------------------------------------------------------
/** * ----------------------------------------------------------------------- * Filename: BaseController.php * Location: ./app/Controllers/BaseController.php * ----------------------------------------------------------------------- */
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
music_ed
Newbie
-
Posts: 5
Threads: 2
Joined: Nov 2020
Reputation:
0
Thank you for your reply, but that doesn't solve my problem. For the sake of the example I didn't post the complete helper but it's constructed as a class, not as a bunch of functions. Now if I implement it by including 'php2jquery' in the $helpers I can't call it. like this:
PHP Code: $data['jqueryparam'] = $royalaudioparam->php_array_to_jquery_param($param,3,"new FWDRAP","FWDRAPUtils.onReady(function(){");
it gives me an Error: "Class 'App/Controllers/php2jquery' not found" So it seeks in the wrong namespace? (I can resolve the namespace by not using it as a helper, but it should be possible as a helper or am I missing something?
this works:
PHP Code: <?php //BaseController namespace App\Controllers; use CodeIgniter\Controller;
class BaseController extends Controller {
protected $helpers = ['php2jquery']; // etc }
?> <?php //Controllers/Test.php namespace App\Controllers; use App\Helpers\php2jquery; class Test extends BaseController {
public function index() { //Variables should be inited by now. $royalaudioparam=New php2jquery(); $data['jqueryparam'] = $royalaudioparam->php_array_to_jquery_param($param,3,"new FWDRAP","FWDRAPUtils.onReady(function(){"); //do something } } ?> // <?php //Helpers/php2jquery.php namespace App\Helpers; class php2jquery { if ( ! function_exists('phpArrayToJqueryParam')) { function php_array_to_jquery_param($param,$indent=0, $object="", $wrapfunction=""){ $jquery_param=""; $close=""; if ($indent<3) $indent=3; $myindent=$this->indent($indent);// Inspringdiepte zetten // logic removed return rtrim($jquery_param, ",".$mynewobjectindent).PHP_EOL.$close; //Laatste comma eraf halen en afsluiten } } if ( ! function_exists('indent')) { function indent($pos=0) { $myindent=""; for ($times=0; $times<=$pos; ++$times){ //echo "pos=".$times.PHP_EOL; $myindent .="\t"; } return $myindent; } } } //class php2jquery
but it should be possible as helper or not?
-
InsiteFX
Super Moderator
-
Posts: 6,575
Threads: 331
Joined: Oct 2014
Reputation:
240
Then you should place it in Libraries or ThridParty, You need to use the use clause above the class name.
PHP Code: // Libraries use app\Libraries\php2jquery;
// ThirdParty use app\ThirdParty\php2jquery;
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
vitnibel
Junior Member
-
Posts: 37
Threads: 0
Joined: Apr 2020
Reputation:
0
With the appearance of namespaces in CI4, the location of certain libraries or helpers has become rather conditionaly.
Let's say you want to make a helper class and place it in the app/Helper directory
in file app/Helpers/Php2jquery.php
PHP Code: <?php
namespace App\Helpers;
class Php2jquery { function phpArrayToJqueryParam($param, $indent = 0, $object = "", $wrapfunction = "") { return 'test'; } }
in file app/Controllers/Test.php
PHP Code: <?php
namespace App\Controllers;
use App\Helpers\Php2jquery;
class Test extends BaseController {
public function index() { $jqueryparam = new Php2jquery();
$data[‘jqueryobject’] = $jqueryparam->php_array_to_jquery_param($param, 4, "new FWDRAP", "FWDRAPUtils.onReady(function(){" );
return view('index', $data); } }
Please note that the file and class name starts with a capital letter as recommended in the manual
-
music_ed
Newbie
-
Posts: 5
Threads: 2
Joined: Nov 2020
Reputation:
0
Thank you for your help. I’ve got it working now.
|