How to configure routing in Codeigniter for Rest server? - codeigniter

I use the following library "Codeigniter Rest Server":
https://github.com/chriskacerguis/codeigniter-restserver
I have a standart controller:
class Messages extends REST_Controller
{
public function dialogs(){
echo "Test";
}
}
I try to call this method from URL:
http://localhost/api/index.php/messages/dialogs
Where messages - controller and dialogs - method
I get error:
{"status":false,"error":"Unknown method"}

CodeIgniter does some simplification for you: the location of the controller "Messages" with the method "dialogs" is automatically given an address of:
http://localhost/api/messages/dialogs. It seems that the REST_Controller has a "_remap" method to redirect your dialogs() function back up into the default CodeIgniter path
This could change depending on where the "application" folder is placed, I'm assuming it's inside the folder api on your localhost

As mentioned by #Tpojka in the comment, you need to specify the method in the method.
Eg:
class Messages extends REST_Controller
{
public function dialogs_get(){
echo "Test";
}
}
You can call this API in POSTMAN using the method 'GET' and URL endpoint will be
http://localhost/api/messages/dialogs

Related

How to Call a controller function in another Controller in Laravel 5

im using laravel 5.
I need to call a controller function but this should be done in another controller.
I dont know how to do this
public function examplefunction(){
//stuff
}
And i have a Route for this function, so at
public function otherfunctioninothercontroller(){
// I need examplefunction here
}
how Can i do this?
1) First way
use App\Http\Controllers\OtherController;
class TestController extends Controller
{
public function index()
{
//Calling a method that is from the OtherController
$result = (new OtherController)->method();
}
}
2) Second way
app('App\Http\Controllers\OtherController')->method();
Both way you can get another controller function.
If they are not in the same folder, place use namespace\to\ExampleClass; on top of your file, then you are able to instantiate your controller.
You can simply instantiate the controller and call the desired method as follows
FirstController.php:
namespace App\Http\Controllers;
class FirstController extends Controller {
public function examplefunction() {
// TODO: implement functionality
}
}
SecondController.php:
namespace App\Http\Controllers;
class SecondController extends Controller {
public function test() {
$object = new FirstController();
$object->examplefunction();
}
}
Now, after i've answered the question, i would like to add the following comment:
Controllers are classes, all rules that applies to normal classes can be applied to them
However, instantiating a controller directly inside another controller to call a desired method signifies a problem in your design for the following 2 reasons:
A controller cannot obtain an instance of another controller directly
Controller should contain as little business logic as possible, and if possible none
The closest possible solution to what you want (WITHOUT BREAKING MVC) is to make an HTTP request to the route that points to the desired method (using cURL, for example) and read the response as the returned data
But this still doesn't make much sense in this scenario because after all you're making an HTTP request from a method in a controller in your project on your server to a method in a controller in your project on your server, seems like unnecessary overhead, right ?
As i said earlier, a controller should contain as little business logic as possible because the logic should stay inside specialized classes (commonly known as Service Classes), and when a processing is requested the controller simply delegates the job of processing to the appropriate service class which does the processing and returns the results to the controller which in turn sends it back as a response
Now imagine if you've the following scenario:
We've got an application that consists of 3 functionalities:
A user can register an account from web application
There's a mobile application that talks to an API to register a user
There's an admin panel, which he can use to add new user
Obviously you need to create 3 controllers, but those controllers contains repeated logic, would you copy/paste the code everywhere ?
Why not encapsulate this logic inside a service class and call it from the controller when needed ?
Let's say I have Controller1 and Controller2. I want to call a function of Controller1 from inside a function placed in Controller2.
// Controller1.php
class Controller1 {
public static function f1()
{
}
}
And on the other controller:
// Controller2.php
use App\Http\Controllers\Controller1;
class Controller2 {
public function f2()
{
return Controller1::f1();
}
}
Points to be noted:
f1() is declared static
A call to a controller from inside another controller is a bad idea. There is no sense of meaning of controllers then. You should just redirect to web.php to save safe whole architecture like this:
class MyController {
public function aSwitchCaseFunction(Request $requestPrm){
...
//getting path string from request here
...
switch($myCase){
case CASE_1:
return redirect()->route('/a/route/path');
....
}
}
}

Pass data from routes.php to a controller in Laravel

I am attempting to create a route in Laravel for a dynamic URL to load a particular controller action. I am able to get it to route to a controller using the following code:
Route::get('/something.html', array('uses' => 'MyController#getView'));
What I am having trouble figuring out is how I can pass a variable from this route to the controller. In this case I would like to pass along an id value to the controller action.
Is this possible in Laravel? Is there another way to do this?
You are not giving us enough information, so you need to ask yourself two basic questions: where this information coming from? Can you have access to this information inside your controller without passing it via the routes.php file?
If you are about to produce this information somehow in your ´routes.php´ file:
$information = WhateverService::getInformation();
You cannot pass it here to your controller, because your controller is not really being fired in this file, this is just a list of available routes, wich may or may not be hit at some point. When a route is hit, Laravel will fire the route via another internal service.
But you probably will be able to use the very same line of code in your controller:
class MyController extends BaseController {
function getView()
{
$information = WhateverService::getInformation();
return View::make('myview')->with(compact('information'));
}
}
In MVC, Controllers are meant to receive HTTP requests and produce information via Models (or services or repositores) to pass to your Views, which can produce new web pages.
If this information is something you have in your page and you want to sneak it to your something.html route, use a POST method instead of GET:
Route::post('/something.html', array('uses' => 'MyController#getView'));
And inside your controller receive that information via:
class MyController extends BaseController {
function getView()
{
$information = Input::get('information');
return View::make('myview')->with(compact('information'));
}
}

RESTful service in CodeIgniter

I am new to CodeIgniter, I want to build restful webservices using CodeIgniter. How can I post data to mysql and fetch from it back using REST services? I have gone through a web site 'tutplus', but it is explained there without mysql database.
Very simply, create some controllers, for example: user.php
At this controller write all necessary methods, exp: getUserInfo(), getUser(), getAge() and call these methods via URL.
To deal with this suggest:
https://ellislab.com/codeigniter/user-guide/general/controllers.html
https://ellislab.com/codeigniter/user-guide/general/routing.html
Actually on the project that i'm working on, we use RESTControllers, there's a project out there on github which extends codeigniter controller with all the REST capabilities:
https://github.com/chriskacerguis/codeigniter-restserver
All you need to do in your code it's include the file on the controller and extend that new controller:
require(APPPATH.'/libraries/REST_Controller.php');
class system extends REST_Controller {
}
You could also include the REST controller on the autoload libraries confinguration.
This library opens the 4 basic REST API as the GET, POST, PUT and DELETE
In your code the controller url should be declarated as this, so you would get a POST method on the index
public function index_post()
{
// ...just some code example
$this->response($book, 201); // Send an HTTP 201 Created
}
If you need a get on the index, you declare it as a GET method:
public function index_get()
{
// ...just some code example
$this->response($book, 201); // Send an HTTP 201 Created
}

Symfony2, check if an action is called by ajax or not

I need, for each action in my controller, check if these actions are called by an ajax request or not.
If yes, nothing append, if no, i need to redirect to the home page.
I have just find if($this->getRequest()->isXmlHttpRequest()), but i need to add this verification on each action..
Do you know a better way ?
It's very easy!
Just add $request variable to your method as use. (For each controller)
<?php
namespace YOUR\Bundle\Namespace
use Symfony\Component\HttpFoundation\Request;
class SliderController extends Controller
{
public function someAction(Request $request)
{
if($request->isXmlHttpRequest()) {
// Do something...
} else {
return $this->redirect($this->generateUrl('your_route'));
}
}
}
If you want to do that automatically, you have to define a kernel request listener.
For a reusable technique, I use the following from the base template
{# app/Resources/views/layout.html.twig #}
{% extends app.request.xmlHttpRequest
? '::ajax-layout.html.twig'
: '::full-layout.html.twig' %}
So all your templates extending layout.html.twig can automatically be stripped of all your standard markup when originated from Ajax.
Source
First of all, note that getRequest() is deprecated, so get the request through an argument in your action methods.
If you dont want to polute your controller class with the additional code, a solution is to write an event listener which is a service.
You can define it like this:
services:
acme.request.listener:
class: Acme\Bundle\NewBundle\EventListener\RequestListener
arguments: [#request_stack]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onRequestAction }
Then in the RequestListener class, make a onRequestAction() method and inject request stack through the constrcutor. Inside onRequestAction(), you can get controller name like this:
$this->requestStack->getCurrentRequest()->get('_controller');
It will return the controller name and action (I think they are separated by :). Parse the string and check if it is the right controller. And if it is, also check it is XmlHttpRequest like this:
$this->requestStack->getCurrentRequest()->isXmlHttpRequest();
If it is not, you can redirect/forward.
Also note, that this will be checked upon every single request. If you check those things directly in one of your controllers, you will have a more light-weight solution.

Laravel - Request::server('HTTP_HOST') returns 'localhost' from within a helper class

I want to get the current domain, using Request::server('HTTP_HOST') - however when I call this from within a helper class it comes back as 'localhost' which is not what I want. From a controller it works as expected. Is there a way to access this information from within a helper class?
The helper class looks like this:
class ApiWrapper {
public static function call($model, $method='', array $input) {
$domain = Request::server('HTTP_HOST');
}
}
You can do URL::to('/') to get the base URL of the Laravel application, if that's what you're asking for. If doing Request::server('HTTP_HOST') from your controller is giving you the desired result, doing the same from the helper class shouldn't be any different.

Resources