I am creating a rest service. I have done all the required methods, what I want to do is some authorization. I have created the table where I store the api-keys, i load them in each method, and it works quite well.
What I need now, is to do some before action that would be called before each method, so i don't have to check if the user is successfully authorized on each method? In simple CI_Controller or in FuelPHP that can be done using public function before, but I dont know how to achieve the same thing in REST_Controller?
Thank you in advance
Here are two controllers. May give you some idea
class MY_Controller extends CI_Controller
{
protected $before_filter = array();
protected $after_filter = array();
private function run_filter($who, $params=array())
{
$filter = $this->{"{$who}_filter"};
if (is_string($filter)) {
$filter = array($filter);
}
if (method_exists($this, "{$who}_filter")) {
$filter[] = "{$who}_filter";
}
foreach ($filter as $method) {
call_user_func_array(array($this, $method), $params);
}
}
public function _remap($method, $parameters)
{
if (method_exists($this, $method))
{
$this->run_filter('before', $parameters);
$return = call_user_func_array(array($this, $method),$parameters);
$this->run_filter('after', $parameters);
}else{
show_404();
}
}
}
class MY_Controller extends CI_Controller
{
public $before_filter = array('check_login');
public $after_filter = array();
private function dashboard()
{
/* other code here */
}
public function check_login()
{
/* Login checking Code here */
}
}
Related
I'm using repository pattern in my Laravel project.
what is the good pattern to call service from other service?
For example service will looks like this:
class GetAllUsersService
{
private $userRepository;
public function __construct(UserRepository $repository)
{
$this->userRepository = $repository;
}
public function execute()
{
return $this->userRepository->getAll();
}
}
Now if I want to execute this service from other part of the application I will do something like this:
class AnyClass
{
public function executeUserService()
{
$repository = new UserEloquentRepository();
$service = new GetAllUsersService($repository);
return $service->execute();
}
}
Is it correct way to do it? Is there other ways? Maybe some UI layer should be in between?
I think you have three ways to do it:
1) use method __construct();
class AnyClass
{
private $get_all_users_service;
public function __construct(GetAllUsersService $get_all_users_service)
{
$this->get_all_users_service = $get_all_users_service;
}
public function index()
{
$fetchAllUsers = $this->get_all_users_service->fetchAll();
}
}
2) use the specified services like a parameter of each function needs to use them:
class AnyClass
{
public function index(GetAllUsersService $get_all_users_service)
{
$fetchAllUsers = $get_all_users_service->fetchAll();
}
}
3) use method app() of Laravel helper like this:
class AnyClass
{
public function index()
{
$get_all_users_service = app(GetAllUsersService::class);
$fetchAllUsers = $get_all_users_service->fetchAll();
}
}
I need to use a session information in various functions of my controller, but I can't initialize it in the constructor, because I get an error. Message: Undefined property: Soporte::$session
class Soporte extends MY_Controller {
function __construct(){
parent::__construct( $module, $functionality );
}
public function actualizarSolicitud( $id_solicitud ){
$session_data = $this->session->userdata('session_user');
$user = $session_data['usuario'];
...
}
public function adminHistorico(){
$session_data = $this->session->userdata('session_user');
$user = $session_data['usuario'];
$config = array();
...
}
...
}
There's a way to initialize a global variable $user?
Try like below, model is quite complicated so I'm not providing it's code, but you should get the point. Any questions let me know.
/**
* This class is used for performing all read/write session operations
* Native php session is utilized (MY_Session library)
*/
class SessionManager extends BaseLibrary {
private $oUser;
public function __construct() {
parent::__construct();
$this->CI->load->model('User');
}
public function setUser(User $oUser) {
$this->CI->session->set_userdata('userId', $oUser->getId());
}
public function getUser() {
if ($this->oUser === null) {
$this->oUser = new User();
if ($this->CI->session->userdata('userId')) {
$this->oUser->setId($this->CI->session->userdata('userId'));
}
}
return $this->oUser;
}
public function logout() {
$this->CI->session->set_userdata('userId', NULL);
}
}
I am trying to create a new model object from my mvc controller but the page doesn't generate. Is there any reason why I can't do this? Surely I should be able to create an object inside an existing one?
Sorry to be so simplistic, and I know I sound like an idiot, but I'm not sure how to explain what I am doing wrong.
class controller_landing extends controller_base
{
public function __construct($get,$post)
{
parent::__construct($get,$post);
$this->model = new model_landing; <-----problem line here
}
}
abstract class controller_base
{
//store headers
protected $get;
protected $post;
//store layers
protected $view;
protected $model;
protected function __construct($get,$post)
{
//store the header arrays
$this->get = $get;
$this->post = $post;
//preset the view layer as an array
$this->view = array();
}
public function __destruct()
{
//extract variables from the view layer
extract($this->view);
//render the view to the user
require_once('view/'.$this->get['controller'].'_view.php');
}
}
class model_landing extends class_mysqli
{
public function __construct
{
echo "landing model";
}
}
class class_mysqli
{
public function __construct
{
echo "mysqli";
}
}
I don´t know, but I think you are missing brackets.
There
public function __construct
{
echo "landing model";
}
should be
public function __construct()
{
echo "landing model";
}
So I was trying to make a CRUD for my app. I have setup a controller, like this:
class Clients extends CI_Controller {
public function __construct() {
parent::__construct();
session_start();
$this->c = new Client();
$this->u = new User();
}
function index() {
$data['current_view'] = 'client_view';
$data['header'] = 'Меню клиентов';
$data['clients'] = $this->getClients();
$this->load->view('main_view',$data);
}
function getClients() {
$this->u->where('username',$_SESSION['username'])->get();
$c = $this->c->where('user_id',$this->u->id)->get();
return $c;
}
function delClient() {
$this->c->where('client_id', $this->uri->segment(3))->delete();
$this->c->skip_validation()->save();
$this->index();
}
}
However, when I'm trying to perform a delete on a client, i get a db error:
You must use the "set" method to update an entry.
Filename: C:\OpenServer\domains\localhost\system\database\DB_active_rec.php
Line Number: 1174
What might be the cause of this? I found a similar question here, but I don't think that's the case.
EDIT: Client model:
class Client extends DataMapper {
public $has_one = array('user');
public function __construct($id = NULL) {
parent::__construct($id);
}
}
you have not passed the uri segment to the function delClient() and this is your model right...
function delClient($id) {
//$id is the value which you want to delete
$this->c->where('client_id', $id)->delete();
$this->c->skip_validation()->save();
$this->index();
}
I want to create a function in a class that is available for a set of users, but that they won't be able to access. Ex:
class Stuff_for_user {
private $errors;
/*
* private $errors gets modified by private functions
*/
public function get_errors(){ // This is for users to display errors.
return $this->errors;
}
/*something here...*/ function set_errors($str){
$this->errors = $str;
}
}
So far so good, but now I want the parent class to be able to set Stuff_for_User's errors:
class Main_mess {
public index(){
$user_available_data = new Stuff_for_user();
if($big_error)
$user_available_data->set_errors("BIG ERROR!!!");
$this->send_to_users($user_available_data);
}
}
I want only Main_mess to be able to access Stuff_for_User's set_errors() method. Is that possible?
No, that is not possible like that, since Main_mess is not a parent class of Stuff_for_users (and this is probably what you want, looking at what your code actually does). So set_errors has to be public if you want to call it from the outside.
This is not possible how you want to implement it.
Some ideas (i dont know why or how you want to do that but just ideas...):
do set_error($str,$access_key) and let $access_key be an access string only you know!
let Stuff_for_user be in Extended_Stuff_for_user which has the set_error function like:
class Extended_Stuff_for_user {
private $errors;
private $Stuff_for_user;
public function set_errors() {
/* ... */
}
public function getStuffForUser() {
return $this->Stuff_for_user;
}
}
It seems that you are looking for implementation of something called friend class in php. Well .. i'm sorry to tell you this, but it is not possible.
You should look at other possible solutions to your problem.
class SecureContainer{
protected $user = null;
protected $target = null;
public function __construct( $target, $user )
{
$this->target = $target;
$this->user = $user;
}
public function __call( $method, $arguments )
{
if ( $this->user->isAllowed(getType( $this->target ), $method))
{
return call_user_func_array(
array( $this->target, $method), $arguments );
}
}
}
Use it like this:
$something = new UnsecureSomething;
$user = new User( $uid );
$something = new SecureContainer( $something, $user );
This should let you control the access to methods.
Yes it possible but it can be dirty.
Like This.
class Stuff_for_user {
private $errors;
/*
* private $errors gets modified by private functions
*/
public function get_errors(){ // This is for users to display errors.
return $this->errors;
}
/*
This way the child classes of Main will able be to use the set_errors function;
*/
function set_errors($class,$str){
if($class instanceof Main_mess)
{
$this->errors = $str;
}
/*
AndThis way the only Main_mess will be able;
*/
function set_errors($class,$str){
if(get_class($class)=="Main_mess")
{
$this->errors = $str;
}
}
class Main_mess {
public index(){
$user_available_data = new Stuff_for_user();
if($big_error)
$user_available_data->set_errors($this,"BIG ERROR!!!");
$this->send_to_users($user_available_data);
}
}