Custom library available like CI3 |
Hello,
How to initialize custom Library? I have some class in Library, in namespace App\Libraries and get it by namespaces is pretty and works fine, but how to get/assign it to base controller. Somethink like CI3. PHP Code: $controller->{$library}->someMethodInLibrary(); And without modification in 'system' directory. Thanks for advice.
Here's one example. There are probably other ways and v4 development team members might be able to suggest more appropriate approaches. But this works.
This example is done using a fresh install of v4. The only default value changed is that $baseURL in /application/Config/App.php is set with the testing server's URL. No items are removed or added to the file /application/Config/Routes.php Here's a very simple "library" /application/Libraries/Test.php PHP Code: <?php namespace App\Libraries; We will create an instance of this class and use the class method getMessge() in the controller Base /application/Controllers/Base.php PHP Code: <?php namespace App\Controllers; The above controller can be used to extend other controllers and the property $testLib can be accessed in child classes using $this->testLib. The "view" is a hacked... er, revised version of welcome_message.php that comes with a clean install. /applications/Views/test_message.php Code: <!doctype html> Direct your browser to http://yoursite.com/base to see if all the pieces fit together and produce the expected output.
05-24-2018, 02:24 PM
(This post was last modified: 05-24-2018, 02:25 PM by Przem4S. Edit Reason: update code )
That very helpful - thank you.
I created by your idea simple library User - in this library I check by session if user is logged. But I have second problem, when User is not logged then I try redirect him to login page by: PHP Code: return redirect()->to('/admin/user/login'); Do you have any idea how to redirect user from other place (non-controller, like library)?
Many thanks for the complete, working example, it works a treat
![]() I find it far easier to learn by modifying a working example than trying to understand new concepts from a manual. Afterwards I usually return to the manual with fresh knowledge and it usually makes more sense. The only problem I had which was easily overcome was by adding the following to Routes.php (even before renaming the files?) >>> $routes->add('/base', 'C_Base::index'); Modifications: 1. I prefer using uppercase wherever possible because it seems easier to separate my code from PHP scripts. 2. I also renamed all three files by adding prefixes... I know a little bit about reusing Class Methods with the same name but find it tremendously confusing. 3. I tried alternative methods of using extend and added resulting error messages where necessary. 4. The revised View uses PHP highlight_file() to display contents of Library and Controller because it see the Library and Controller 5. Added DECLARE(strict_types=1); because it catches far more errors The revised View: PHP Code: THIS IS THE VIEW I would be grateful if someone could explain the following syntax or a link with details: >>> __CONTRUCT(...$params) (07-09-2018, 09:54 AM)John_Betong Wrote: I would be grateful if someone could explain the following syntax or a link with details: The ...$params takes all arguments given to the method and packs them into a new array called $params. Then, when passed back into parent::__construct(...$params) it unpacks the array so that the arguments are passed along as expected. Doing it this way allows you to not have to worry about the exact parameters in your own controllers. I've been thinking about removing everything from the constructor so it's a little cleaner but haven't made any decisions on that just yet.
FYI - I just pulled that requirement away from the Controller class, so we no longer use the constructor. That makes one less thing to remember when you make your controllers.
(07-09-2018, 09:45 PM)kilishan Wrote: FYI - I just pulled that requirement away from the Controller class, so we no longer use the constructor. That makes one less thing to remember when you make your controllers. Champion, keep it lean, mean no frills and allow users to easily add their own convoluted fancy stuff ![]() With a bit of luck it should also make less testing and the Alpha Release just that little bit sooner! Keep up the good work. |
Welcome Guest, Not a member yet? Register Sign In |