class Api extends CI_Controller {
public function index()
{
show_error("You are not authorized to access this page", 401);
}
I have an api class class Api extends CI_Controller and an another
class myproject extends , now if want to use the functions of the api class in myproject class. how can i do . do i have to create an object of api class or just extend the myproject class with parent::api class . please help me as i m not good at oops.
details - i have made a class "class myproject extends ci_controller" which has different functions for user registration and login application. it all works fine using a single controller. But now want to use an api file.which has functions for login. how can i call those functions in api file from "class myproject"
You can create an helper, like the form one and use your method.
Then you can load it:
$this->load->helper('api');
First of all this looks more like a software design problem rather then a real code issue.
CodeIgniter is based on the MVC concept, and the Controller is only meant to build up the page.
You should probably make a library for all the API functionality, and have the controller calling that library and converting the data to JSON or whatever you want it to be converted to.
For myself i apply a simple rule:
Never write/call a Controller method that doesn't generate any form of output.
I recommend you to stick to the CodeIgniter way of coding, and not try to avoid them.
You should just need to extend the API class, as in
class Myproject extends Api { ... }
but there a few other issues, such as include_once("Api.php"), for example, in the Myproject.php file so that the subclass can "see" the superclass declaration. This thread on the codeigniter forums discusses these issues (sorry for the google webcache version but the forums are down for maintenance at the moment).
Firstly, you can't call a function of a controller in a controller unless and until you are inheriting it.
It's better that you create API class as library, instead of a controller.
If you have some restriction to have it as a controller only then use these APIs through curl calls.
But the best way is to have them in helper, if you are accessing them from your own server only.
Related
i am working on a laravel project .almost i see in each controller these line format :
!auth()->user()->can('something (differ from each controller to other one)')
but in my php editor it says method {can} not found for this object. so i try to found method can and not found it .even i edit __Call magic method to see if when can method is calling does magic function run but i know it never run for can function.so how it is possible to link function to object when it is not defined in class and its all mother class.and where does can function locate?i search and i see laravel has some policy for authorize users but yet i don not know how can function link to user object without
define in classes and even magic method does not run .and how can i develope these type of function (for authorizing in laravel and change policy)
can method is a part of Authorizable trait in Illuminate\Foundation\Auth\Access
Look at your User model, you will found that model is extended from Illuminate\Foundation\Auth\User as Authenticatable. and Authenticatable uses the above trait.
it's part of the core of laravel, you can read the documentation from official docs
I am working on a Yii1 old website. which is linked with some external PHP controllers. These external controllers provide some common functions that are used between 2 different applications. I have a function in Yii model that I want to use in one of the external PHP controller is there a way to do this? Currently, this is done by rewriting MySQL query in the PHP external controller but I don't want to follow this lame practice.
I found this link and I am able to access Yii externally but it's still not very helpful. Using Yii in 3rd-Party Systems
Here's a sample of my code:
namespace main\Helpers;
require_once('path/to/yii.php');
Class HelperClass {
public static function yiisupport($id){
// I am able to access Yii variables using
\Yii::app()->name
// But how to access the yii model or controller functions? I need something like the follwoing
$model = \Yii::app()->YiiModel::model()->findByPK($id);
}
}
Can anyone help?
You need to create Yii application first (using config file path) to access its models and controllers as it is mentioned in the documentation. Then you can access any model class in your external application just like you would access it in your Yii application, and you can use controller actions as below;
$controller = new \YOURController('ACTION_NAME');
$controller->ACTION_NAME();
If you have already imported models/controllers in your config file then you will not need to import any class but if you have not then you can import specific model/controller like below;
\Yii::import('application.models.MODEL_NAME');
\Yii::import('application.controllers.CONTROLLER_NAME');
Check the examples below;
namespace main\Helpers;
require_once('path/to/yii.php');
\Yii::createWebApplication('path/to/config.php');
Class HelperClass {
public static function yiisupport($id){
// Access Yii variables
\Yii::app()->name;
// Access yii model
$model = \YiiModel::model()->findByPK($id);
// Access yii controller and its actions
$controller = new \YiiController('actionCreate');
$controller->actionCreate();
}
}
Update:
As mentioned by #rob006 in the comment below that calling yii controller action outside Yii application is a bad idea, however if you still want to do that, there is a safer way which follows the Yii application lifecycle and this way access filters and beforeAction() will be triggered. So you can call controller action in a safer way as below;
\Yii::app()->runController('route/to/action');
Hi I have a file called payment.php it contains some functions related to payments and some multiple classess. So I want to include that file in my symfony2 Controller to access its all methods and classless. I am trying it as follows:
//File location is in src/AppBundle/Controller/payment.php
namespace AppBundle\Controller;
require_once __DIR__.'/./payment.php';
//My controller
class ApiServicesController extends Controller
{
$this->payment(array('txnId'=>1112548));
}
But I am not able to access the file and its methods.
I am using this approach because keeping it in /vendor directory it also not able to access because this file contains multiple classless in same files.
Please advice me how can I access this file in my controller.
thanks in advance
If paymant.php have classes you need to make instance of that class to call method from it, that's like basic OOP stuff.
class ApiServicesController extends Controller
{
$this->payment(array('txnId'=>1112548));
}
First of all, where is the method in this controller where you want to call your method? Then, why you are calling payment on $this if it comes from diffrent class. It should be something like this
class ApiServicesController extends Controller
{
public function indexAction()
{
$somePaymentClass = new SomePaymantClass(); //whatever class you want from payment.php
$somePaymentClass->payment(array('txnId'=>1112548));
}
}
But iI strongly recommend to use it as a service and put it in some autoloader namespace.
You have to make a Payment class as a service and then you can use all functions of Payment class in controller. Please refer this document.
http://symfony.com/doc/current/service_container.html
I installed the Bed Edmund's Ion Auth login script and would like to make the functions global throughout my application.
I installed the code and it works great, but I'm having issues extending it. The ion Auth controller extends CI_Controller, so then in all my other controllers I extend Auth.
//Ion Auth Controller Class
class Auth extends CI_Controller {
and in a separate page:
//Home Page Controller Class, where I will be placing the login form
require_once("auth.php");
class Home extends Auth {
By doing this, I get the following PHP error:
Message: Assigning the return value of new by reference is deprecated
I read in the CodeIgniter docs that extended classes must now begin with MY_, so I tried that as well with no luck.
What am I doing wrong? Is my logic all wrong?
Do a CTRL-F for =& inside the Auth class source code (the one you installed) and replace every instance by =.
$foo =& new Bar();
is a deprecated syntax that was used in PHP4 but no longer in PHP5.
I am currently developing a Lithium application and have come across a function that I have written that I would like to use across multiple Controllers.
I obviously don't want to have the function in each controller. What is the standard way of creating a re-usable component in Lithium?
The lack of search facility on their documentation is making it difficult to find any specifics.
You could try extending controller. Extending controllers is not that bad according to core devs. If that is not and option you could extract your code into a plugin, still some code in controller though.
All you have to do is create a extensions/action/Controller.php and have your controllers extend that.
In your extensions/action/Controller.php
<?php
namespace app\extensions\action;
class Controller extends \lithium\action\Controller {
protected function _init() {
parent::_init();
//add your functionality here
}
}
?>
And then, your controller has to extend the above mentioned base controller: class MyController extends \app\extensions\action\Controller {
I think this is not a Lithium-specific thing. You could either inherit from the Controller and make your own base controller, but you can also create arbitrary classes that hold your functionality. Don't let a framework inhibit you =)
Regarding the documentation: I usually google in the sense of "<keywords> site:lithify.me"