Welcome Guest, Not a member yet? Register   Sign In
Where is the right place and way
#1

Hi,

i want to include a kind of library to check login informations, to handle some basic functions etc. So in the library i need db-support too.

This library should be avivable on all pages and automatically also in models.

As a library i need to add this code in every file where i want to use it (i think)
"use App\Libraries\MyLib;"
and
create a new instance by
"$mylib = new MyLib();"


But i want to have access to the library without adding some code in every file.


If i create it as a "helper", i need to add a new db-connection in every function, that i dont wont too.


It should be a central place. It should be the best place for perfomance (so i dont wont to create x-instances etc.)


So is it possible as a library, or is a "service" or "helper" the better/right way to do it?
Reply
#2

1) Add it to the Autoload or classmap then you should be able to access it.
2) Create a service for it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Do you mean i can create a service from my library?

I found only smal tutorial and docs about creating a own service.

Is it possible to make the library himself globally accessable (like in my post)?
Reply
#4

https://codeigniter.com/user_guide/conce...l#services
This is pretty thorough…
Reply
#5

(This post was last modified: 01-24-2025, 10:55 PM by 4usol.)

thanks,

but i'm so sorry,i dont understand somethings around the services:

- Where i can load or put my classes from the library into the service
The file wich extends the BaseService isnt it, or?

- How i can access the classes from a service and put my parameters to the them?

-Where i need to put the part to get the service and make it avivable into the global space of the project (in all controller, helper, models, views etc) without getting it new.
I mean this part "$ myservice = service('myservice');"

I cant find a simple example to make this thinks clear for me.

In the meantime i find something, what i also dont understand, whats about
Code:
Factories::libraries('MyLibrary);   
$testA = Factories::MyLibrary('functionnameA', ['getShared' => true]); 
$testB = Factories::MyLibrary('functionnameA', ['getShared' => false]);

Is it possible, in my tries it failed?

Also i trie to setup my simple service as the following steps.

1) create my service-file: "Test.php" in /Config
Code:
namespace App\Config;

use CodeIgniter\Config\BaseService;

class Test extends BaseService
{
    public function hello($param){return $param.' is nice';}   
}

2)go to basecontroller (note i want to include the service in a central place to make it globally avivable)
use \Config\Test;
Code:
...
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        echo"Basecontroller";
        // Central start of "the library as service"

        $test= \Config\Test::hello('what ever test');
        $testA = service('test');
        $testB = \Config\Services::test();
        $testC=service('test', 'what ever testC');
        $testD= \Config\Services::test('what ever testD');

        var_dump($test).'<hr>'
        .var_dump($testA).'<hr>'.
        .var_dump($testB).'<hr>'.
        .var_dump($testC).'<hr>'.
        .var_dump($testD).'<hr>';
...
}

in no one is the result of the test-function from the file Test.php - the expectation would have been that "hello what else..."  would be displayed there

What i do wrong?
Reply
#6

(This post was last modified: 01-24-2025, 11:25 PM by InsiteFX.)

app/ThirdParty
---- GoogleTranslate.php

app/Config/Services
PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Config\BaseService;
use 
GoogleTranslate;

/**
 * Services Configuration file.
 *
 * Services are simply other classes/libraries that the system uses
 * to do its job. This is used by CodeIgniter to allow the core of the
 * framework to be swapped out easily without affecting the usage within
 * the rest of your application.
 *
 * This file holds any application-specific services, or service overrides
 * that you might need. An example has been included with the general
 * method format you should use for your service methods. For more examples,
 * see the core Services file at system/Config/Services.php.
 */
class Services extends BaseService
{
    public static function translate(bool $getShared true): object
    
{
        if ($getShared) {
            return static::getSharedInstance('translate');
        }

        return new GoogleTranslate();
    }


app/Helpers
---- language_helper.php

PHP Code:
/**
 * translateCategories ()
 * -----------------------------------------------------------------------
 * app/Config/Autoload.php
 * Add:
 * public $classmap = [
 *    'GoogleTranslate' => APPPATH . 'ThirdParty/GoogleTranslate.php',
 * ];
 *
 * NOTE:
 * Maximum of 5000, character limit per translate!
 *
 * If too long you can split the string in half.
 */
if ( ! function_exists('translateCategories'))
{
    /**
     * @param  string $source
     * @param  string $target
     * @param  string $text
     * @return string
     * @throws Exception
     */
    function translateCategories(string $source ''string $target ''string $text ""): string
    
{
        $gTranslate service('translate');

        return $gTranslate->translate($source$target$text);
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(This post was last modified: 01-25-2025, 10:00 AM by 4usol.)

thanks a lot, for me it will more helpful if i understand what i doing wrong in my simple example instead to look into another example. May its possible to give me a feedback to my own example?

If i look at your example i have the following questions.

- Is it a must to use the sample-file (Config/Services.php) and call the class-name who extends the BaseService as "Services"?
- Is it possible to load my library in the same way like the third party file into a service - in my case by
"use App\Libraries\MyLibname;"?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB