Welcome Guest, Not a member yet? Register   Sign In
Use Validation Rules without loading a page from a controller
#12

[eluser]esra[/eluser]
[quote author="Brian Bommarito" date="1184810756"]esra has it right.

I want to include a block of code to do the quick login on a number of pages. Now, looking at validation, I don't know if it would work without having the logic in all the views it's in because you have to perform the run() on it, and without knowing exactly where it is (IE what page it's on) I can't code that properly.[/quote]

Use Coolfactor's View library. The part method in the View library can be used to load multiple blocks and module-specific view fragments. The view fragment for the login block could have a dedicated helper which you could use to handle your block logic. This does not break CI's approach to MVC. You might do some searches on the forum to learn how to run a database query from a helper.

With the Coolfactor's View library, you can restructure your modules into module-related views, blocks (which I consider to be shareable resources), and templates. Also, a module could be composed of multiple view fragments and the part method can be used to assemble those module-specific view fragments. Combine this with Zawk's Modular Separation contrbution (i.e., an extended Loader and Router library), and you should have a CMS/portal like directory structure. I modified Zawk's Loader to handle blocks and templates as separate flavors of views for application directory storage reasons (i.e., I store blocks in application/blocks/ and templates in application/templates/). Aside from storing blocks and templates in separate directory structures, this approach allows you to install and uninstall blocks and templates easily (Just add or remove the directories as needed).

My modified view method in Zawk's Loader library is below:

Code:
function view($view, $vars = array(), $return = false)
{
  $module = '';
  $CI = &get;_instance();

  // Is the requested view a module view?
  if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(1))) {
   $module = $CI->uri->segment(1);
  }

  // Is the requested view a module view in a sub-folder?
  if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(2) . '/controllers/' . $CI->uri->segment(1))) {
   $module = $CI->uri->segment(2);
  }

  // Return the view request as a module view
  if (file_exists(APPPATH . 'modules/' . $module . '/views/' . $view . EXT)) {
   $view = '../modules/' . $module . '/views/' . $view;
  }

  // Return the view request as a template
  if (file_exists(APPPATH . 'templates/' . $view . '/' . $view . EXT)) {
   $view = '../templates/' . $view . '/' . $view;
  }

  // Return the view request as a block
  if (file_exists(APPPATH . 'blocks/' . $view . '/' . $view . EXT)) {
   $view = '../blocks/' . $view . '/' . $view;
  }

  return parent::view($view, $vars, $return);
}

To use the above, your template directory names under templates/ need to have the same name as the template. For example, a template called aria needs to be stored in templates/aria/aria.php. Similarly, a block called login needs to be stored in blocks/login/login.php.

You need to be very aware of naming conflicts because views, view fragments (blocks), and templates do not have separate namespaces. They are all views as far as CI is aware. The above change merely adds additional search paths to Loader, allowing your blocks and templates to be stored separately.

Hopefully, the above should send you in the direction I believe you want to go.


Messages In This Thread
Use Validation Rules without loading a page from a controller - by El Forum - 07-19-2007, 01:36 PM



Theme © iAndrew 2016 - Forum software by © MyBB