Welcome Guest, Not a member yet? Register   Sign In
Creating a custom service/library
#1

Hi there,

I'm a newbie to CI4 so please bear with me. I'm building a small project and am keen on keeping my controllers small so intend to carry out input validation within my project's models.

It has been suggested that it may be good practice to pass back the result from a model's method (such as 'create') as some sort of result object. This object would contain fields to indicate whether the method failed or succeeded and could also contain an array of fields that did not validate, along with user-friendly error messages, etc, etc.

I think this sounds like a good idea. So my understanding is that each model should really tie to a field in the database so I shouldn't be creating a new result model. Instead it looks like I should be creating a result service and passing it into a model's method as a dependency.

I've read the CI4 documentation on creating services and could do with a little bit of guidance by way of an example. Or, tell me this is the wrong way of doing this.

Any help or pointers would be greatly appreciated.
Reply
#2

Assuming you define validationRules in the model, you need to use something like the following in your controller method:

PHP Code:
    if (!$this->validate($model->validationRules)) {
      $errors $this->validator->getErrors());
      //... do something with the errors array.
    
Reply
#3

(02-05-2020, 08:30 AM)x1250 Wrote: Assuming you define validationRules in the model, you need to use something like the following in your controller method:

PHP Code:
    if (!$this->validate($model->validationRules)) {
      $errors $this->validator->getErrors());
      //... do something with the errors array.
    

Thanks for your reply. This is a solution, but is not the one I am looking for. I guess if I take a more generic example (and please assume I'm a newbie) it may illustrate where I'm struggling.

I want to create a new class that has its own properties and methods, but has nothing to do with the tables in my database. Do I still create it as I would any other model, or should I use a service/library? If it is a service/library a very short example would help, because I'm struggling to understand how to do it from the documentation.

Apologies if this comes across as a remedial quesion.
Reply
#4

(This post was last modified: 02-05-2020, 04:31 PM by littlej.)

Hello Fido L Dido !

I have an application in which I needed to manage measurement conversions (inches to centimeters, meters to feet, inches to feet...). So I created an independent library, because I can use it in other apps. And it does not use a database.

1 / The class is located here: "app/Libraries/Snippets/conversion.php". It uses the namespace: "namespace Snippets;" (at the beginning of the file, just after "<?php "), which is the folder's name.
2 / In the file "app/Config/autoload.php" I added the namespace to the $psr4 variable:

PHP Code:
$psr4 = [
  'Config'      => APPPATH 'Config',
  APP_NAMESPACE => APPPATH,                // For custom namespace
  'App'         => APPPATH,                // To ensure filters, etc still found,
  'Snippets'    => APPPATH 'Libraries/Snippets' // The namespace I use in step 1 => The folder's location
]; 

3 / When I need it, I use it like this:

PHP Code:
$conversion = new \Snippets\Conversion();
$lenghtConversion $conversion->lengthimperialtometric(10,'in','cm');

// "Conversion()" is the name of the class 
// The method "lengthimperialtometric" requires: a value (10), the initial unit (inches), the desired unit (centimeters)
// The result is: $lengthConversion = 25.4; 

Hope it helps you find a solution ;-)
Reply
#5

(02-05-2020, 04:07 PM)littlej Wrote: Hello Fido L Dido !

I have an application in which I needed to manage measurement conversions (inches to centimeters, meters to feet, inches to feet...). So I created an independent library, because I can use it in other apps. And it does not use a database.

1 / The class is located here: "app/Libraries/Snippets/conversion.php". It uses the namespace: "namespace Snippets;" (at the beginning of the file, just after "<?php "), which is the folder's name.
2 / In the file "app/Config/autoload.php" I added the namespace to the $psr4 variable:

PHP Code:
$psr4 = [
  'Config'      => APPPATH 'Config',
  APP_NAMESPACE => APPPATH,                // For custom namespace
  'App'         => APPPATH,                // To ensure filters, etc still found,
  'Snippets'    => APPPATH 'Libraries/Snippets' // The namespace I use in step 1 => The folder's location
]; 

3 / When I need it, I use it like this:

PHP Code:
$conversion = new \Snippets\Conversion();
$lenghtConversion $conversion->lengthimperialtometric(10,'in','cm');

// "Conversion()" is the name of the class 
// The method "lengthimperialtometric" requires: a value (10), the initial unit (inches), the desired unit (centimeters)
// The result is: $lengthConversion = 25.4; 

Hope it helps you find a solution ;-)

This really helps, thank you   Smile
Reply
#6

Great ! ;-) If you have time, don't forget to give me some reputation by clicking on the "Rate" button below :-D
Reply




Theme © iAndrew 2016 - Forum software by © MyBB