Codeigniter - My Model Jamie Rumbelow + HMVC callback validation in model - codeigniter

i wonder how to use callback validation using MY model - made by Jamie Rumbelow with Modular extension - wiredesignz
i have create MY_Form_validation and modified the run function like this:
class MY_Form_validation extends CI_Form_validation {
/**
* Stores the CodeIgniter core object.
*
* #access public
*
* #var object
*/
public $CI;
/**
* Constructor
*
* #return void
*/
function __construct($config = array())
{
// Merged super-global $_FILES to $_POST to allow for better file validation inside of Form_validation library
$_POST = (isset($_FILES) && is_array($_FILES) && count($_FILES) > 0) ? array_merge($_POST,$_FILES) : $_POST;
parent::__construct($config);
}
/**
* Performs the actual form validation
*
* #access public
*
* #param string $module Name of the module
* #param string $group Name of the group array containing the rules
*
* #return bool Success or Failure
*/
public function run($module='', $group='')
{
(is_object($module)) AND $this->CI =& $module;
return parent::run($group);
}
it work perfectly if i run form validation with callback in controller, but i wonder how to run in inside a model, it always skip the callback
thank you for your time, and your answer :)

Related

Call to undefined method Illuminate\\Auth\\Access\\Response::authorize()

I'm trying to use Response::authorize() in ContactPolicy.php
but I got this error
Call to undefined method Illuminate\Auth\Access\Response::authorize()
<?php
namespace App\Policies;
use App\models\User;
use App\Contact;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;
class ContactPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can permanently delete the contact.
*
* #param \App\models\User $user
* #param \App\Contact $contact
* #return mixed
*/
public function forceDelete(User $user)
{
return $user->role === 'admin'
? Response::allow()
: Response::authorize();
}
}
I visit this website to check Response class Illuminate/Auth/Access/Response.html
then I checked file in my project vendor\laravel\framework\src\Illuminate\Auth\Access\Response.php
in my files I didn't find it ... all i find is this
<?php
namespace Illuminate\Auth\Access;
class Response
{
/**
* The response message.
*
* #var string|null
*/
protected $message;
/**
* Create a new response.
*
* #param string|null $message
* #return void
*/
public function __construct($message = null)
{
$this->message = $message;
}
/**
* Get the response message.
*
* #return string|null
*/
public function message()
{
return $this->message;
}
/**
* Get the string representation of the message.
*
* #return string
*/
public function __toString()
{
return (string) $this->message();
}
}
how this file miss the rest of methods the laravel docs talk about ??
tihs method requires laravel >= 6.x
this method not available in laravel 5
so make sure the laravel version in your project matches the laravel version that you browsing in laravel docs or in laravel API

Query to all contents of tabel in related model in laravel

Assume we have a model name "Article" in Laravel and want to make query for retrieving latest articles, so one way is to define a method in the "Article" model like this:
public function newArticle()
{
return static::where('created_at', '>', Carbon::subMonths(1));
}
The question is, why we should use
static::
in the above code?
Is it possible to use
$this or self::
instead of
"static::" ?
Thanks in advance,
You could but there is not interest because:
where method does not exist statically on Illuminate\Database\Eloquent\Model class, so it calls __callStatic magic method which delegates the call to an instance
/**
* Handle dynamic static method calls into the method.
*
* #param string $method
* #param array $parameters
* #return mixed
*/
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}
It calls where method on instance but it does not exist either, so it calls __call magic method which delegates it an Illuminate\Database\Eloquent\Builder instance.
/**
* Handle dynamic method calls into the model.
*
* #param string $method
* #param array $parameters
* #return mixed
*/
public function __call($method, $parameters)
{
if (in_array($method, ['increment', 'decrement'])) {
return $this->$method(...$parameters);
}
return $this->forwardCallTo($this->newQuery(), $method, $parameters);
}
/**
* Get a new query builder for the model's table.
*
* #return \Illuminate\Database\Eloquent\Builder
*/
public function newQuery()
{
return $this->registerGlobalScopes($this->newQueryWithoutScopes());
}

Custom generator command, is not stopping when create file that already exists

So, i'm trying to use PHP Artisan on Laravel 5.3 to create a class file for each Cron configuration in my project, i'm doing this because it's possible that i'll want to create these files from a separate GUI in the future.
I'm able to create the files, and i'm using stubs so everything gets generated as it should, the problem however is that for some reason, if a file, say "cron_4" exists and i call my custom command php artisan make:cron cron_4 it'll allow me to do so and will simply overwrite the existing file.
This is my code so far. Any ideas as to what i might be doing wrong here?
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
class CronMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* #var string
*/
protected $name = 'make:cron';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create a new Cron class';
/**
* The type of class being generated.
*
* #var string
*/
protected $type = 'Cron';
/**
* Get the stub file for the generator.
*
* #return string
*/
protected function getStub()
{
return __DIR__.'/stubs/cron.stub';
}
/**
* Get the default namespace for the class.
*
* #param string $rootNamespace
* #return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Crons';
}
/**
* Execute the console command.
*
* #return void
*/
public function fire()
{
if (! $this->option('id')) {
return $this->error('Missing required option: --id');
}
parent::fire();
}
/**
* Replace the class name for the given stub.
*
* #param string $stub
* #param string $name
* #return string
*/
protected function replaceClass($stub, $name)
{
$stub = parent::replaceClass($stub, $name);
return str_replace('dummy:cron', 'Cron_' . $this->option('id'), $stub);
}
/**
* Determine if the class already exists.
*
* #param string $rawName
* #return bool
*/
protected function alreadyExists($rawName)
{
return class_exists($rawName);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return [
['id', null, InputOption::VALUE_REQUIRED, 'The ID of the Cron being Generated.'],
];
}
}
I figured it out, it was my custom code that was to blame
/**
* Determine if the class already exists.
*
* #param string $rawName
* #return bool
*/
protected function alreadyExists($rawName)
{
return class_exists($rawName);
}
This was overriding the default configurations which made it fail probably because of the $rawName variable.
In my case simply removing this function solved the issue.

Controller RESTful methods in laravel 5

when i executing php artisan make:controller Test command in laravel 5
i am getting below blueprint
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class Test extends Controller
{
//
}
But Documentation said we should get skeleton with RESTful methods
class Test extends BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
So what is wrong in my case . how to get what exactly RESTful Resource Controllers should be ?
I think you have to add --resource option at the end of this command to generate a RESTful controller. Following the official laravel docs, your command should be like this:
php artisan make:controller Test --resource

Zend check if view exist - class does not work

I have found this class:
https://github.com/lloydwatkin/Demos/blob/master/zendframework/renderifexists/RenderIfExists.php
The code looks like this:
<?php
/**
* View helper to render a view file if it exists
*
* #author Lloyd Watkin
* #since 12/12/2010
* #package Pro
* #subpackage View
*/
/**
* View helper to render a view file if it exists
*
* #author Lloyd Watkin
* #since 12/12/2010
* #package Pro
* #subpackage View
*/
class Pro_View_Helper_RenderIfExists
extends Zend_View_Helper_Abstract
{
/**
* Errors
*
* #var string
*/
const INVALID_FILE = 'Invalid file parameter';
/**
* Holds file name for processing
*
* #var string
*/
protected $_file;
/**
* Takes a products options array and converts to a formatted string
*
* #param string $file
* #return string
*/
public function renderIfExists($file)
{
if (!is_string($file) || empty($file)) {
throw new Zend_View_Exception(self::INVALID_FILE);
}
$this->_file = $file;
if (false === $this->_fileExists()) {
return '';
}
return $this->view->render($file);
}
/**
* Check to see if a view script exists
*
* #return boolean
*/
protected function _fileExists()
{
$paths = $this->view->getScriptPaths();
foreach ($paths as $path) {
if (file_exists($path . $this->_file)) {
return true;
}
}
return false;
}
}
I want to check if a view exists; If it does: Show it.
I have placed the file in my /library/. When I call $this->renderIfExists('info-box.phtml'); from the controller I get this error:
Message: Method "renderIfExists" does not exist and was not trapped in __call()
How can I fix this?
Thanks in advance!
to call a view helper from the controller, you should use:
$this->view->renderIfExists()
bit late to this, but the code you've got above is something I wrote. Have you registered the helper path, if w would the framework know where to load the file?

Resources