0
votes

I am trying to create a library that has a set of functions. I want my form, in my view, to be able to access a function and execute it. Below is my php file, which I placed in the App/Lib folder

myClass.php:

class myClass{
       public function  someFunction(){
            $parentPage =$this->referer();
            //do something
            $this->redirect($parentPage);
       }    
}

Then, in my view, I have a form (I am not using form helpers (nor do I want to) that I want to hit my library class, and function, and redirect to a given page (in this example, just the referring page). The problem is, though, that cake always tries to find the function within a controller in the controller folder. How do I tell the form to use a class outside the controller folder?

My view:

 <form id="login-user" action="/Lib/myClass/someFunction" method="post">
   //form stuff here
 </form>

But I get a not found error.

Is this even able to be done?

1
This looks like a typical controller method. Why do you want to create a library class for this functionality?dhofstet

1 Answers

0
votes

you can tell cake what folder path you want to call your views from.

put this in your bootstrap App::build(array('View' => array( $my_lib_folder_path.DS )));

here is a snippet of what i built, to let cake search all folders in my view folder. since i like to arrange my code in folders. i.e View/CompanyManagement/AddCompanies/index.ctp View/CompanyManagement/EditCompanies/index.ctp View/CompanyManagement/DeleteCompanies/disable.ctp View/CompanyManagement/DeleteCompanies/delete.ctp

and my code tells cake to search all files under my view folder, and view sub folders to find a particular view i want. but exclude the layouts, elements and helpers folders from the search.

app/Config/bootstrap.php

define('ELEMENTS',    APP.'View'.DS.'Elements');
define('HELPERS',     APP.'View'.DS.'Helper');
define('LAYOUTS',     APP.'View'.DS.'Layouts');
define('VIEWS',       APP.'View'.DS)


includeViews();

function includeViews() {
    App::uses('Folder', 'Utility');
    $folder        = new Folder(VIEWS);
    $folders_paths = current($folder->read($sort = true, $exceptions = false, $fullPath = true));

    foreach( $folders_paths as $folder_path ) { 
      $isAHelperPath   = (new Folder($folder_path))->inPath(HELPERS);
      $isALayoutPath   = (new Folder($folder_path))->inPath(LAYOUTS);
      $isAnElementPath = (new Folder($folder_path))->inPath(ELEMENTS);
      if ( ! $isAHelperPath && ! $isALayoutPath && ! $isAnElementPath ) {
        App::build(array('View' => array( $folder_path.DS )));
      }
    }
}