yii runcontroller in other controller actions with params - yii-events

// In my Controller A
class Controller A extends Controller
{
public function action save()
{
$array1 = $_POST['array'];
$A = $this->redirect(array('controller B/insert','data'=>$array1));
echo $a;
}
}
// controller B
class Controller B extends Controller
{
public function action Insert($data)
{
echo $data;
/*----code-----*/
return value;
}
}
I am newbie in yii framework. I have problem with, call controller b action Insert() from controller a with parameters but i have error:
"400 Your request is invalid."
I don't know how to fix it. please help me anyone..! Sorry for my English.. Thank You..

You should call the controllerB like this
www.example.com/controller/action/data/somedata
You might have missed the data so its difficult for the system to find the action

Related

Distinguish between a call from web route vs API route?

In my web.php file, I have a route that looks like this:
Route::get('/', 'HomeController#getFeed');
And in my api.php file, I have a route that looks like this:
Route::get('feeds', 'HomeController#getFeed');
Notice that they both call the same method, getFeed().
Is there a way to distinguish whether the call came from the web route vs the API route in the controller's method? I need to be able to return two different responses, one for the web route and one for the API route.
Here is the HomeController.php class:
class HomeController extends Controller
{
public function getFeed() {
$user = Auth::user();
// How to check if call is from web route or API route?
// Need to return two different responses for each scenario.
}
}
Thanks.
All routes from api.php are automatically prefixed with 'api/'
So you can use he below code to check
if (Request::is('api*')) {
echo "request from api route";
exit();
}else{
echo "request from web";
exit();
}
I use \Request::is('api/*')
if(\Request::is('api/*'))
return 'API';
else
return 'Non-API';
Alternatively you can check using the route name;
if(\Request::route()->getName() == 'APIFeed')
return 'API';
else
return 'Non-API';
Make sure to set the route name;
In web.php
Route::get('feeds', 'HomeController#getFeed')->name('WebFeed');
In api.php
Route::get('feeds', 'HomeController#getFeed')->name('APIFeed');
replace this line
Route::get('feeds', 'HomeController#getFeed');
with
Route::get('api/feeds', 'HomeController#getFeed');
means add api prefix in all of your api routes.
it will help you to identify which route come from api and which not.
to check you can use below code
if (Request::is('api*')) {
echo "request from api route";
exit();
}else{
echo "request from web";
exit();
}
You can use
$currentRoute = Illuminate\Routing\Router::getCurrentRoute(); // Returns a Route
$currentRoute->uri(); // returns the uri of the cureent route
https://laravel.com/api/5.4/Illuminate/Routing/Router.html#method_getCurrentRoute
https://laravel.com/api/5.4/Illuminate/Routing/Route.html
Hope this helps. :)
In your shoes, I would have created 3 controllers to handle the requests and like Niraj proposed, separate routes by prefix the api route with /api/
class HomeController extends Controller
{
public function getFeed(entrypoint) {
$user = Auth::user();
// do the common magic here ...
}
}
class WebHomeController extends HomeController
{
public function getFeed() {
feed = this.getFeed();
// do the crazy web magic here ...
}
}
class APIHomeController extends HomeController
{
public function getFeed() {
feed = this.getFeed();
// do the crazy api magic here ...
}
}
Route::get('feeds', 'WebHomeController#getFeed');
Route::get('api/feeds', 'APIHomeController#getFeed');
Here's how you access parent methods from child controller
class WebHomeController extends HomeController
{
//get parent methods and protected variables
public function __construct()
{
parent::__construct();
}
public function getFeed() {
//access parent method
$p = $this->getFeed();
feed = this.getFeed();
// do the crazy web magic here ...
}
}

Deciding which controller#action to be called based on a given parameters

I have to build an api.
It has one route. The client is sending a POST request with an XML.
Based on that xml, I have to decide witch controller#action to be called.
And I have a lot of controllers.
Unfortunately I can't modify the client side.
Do you have any suggestion how can i do that in a Laravel way?
For example
POST["body"] =
"...
<controller>content</controller>
<action>index</action>
..."
I want to call a ContentController::index()
Thx!
Thx for the reflection stuff. It is a big magic, worth the effort to look into it deeper.
I have no problem to parse the xml. So here is a simplier example
URL: /api/request/content/show
Routes.php
Route::get('api/request/{controller}/{action}', 'ApiController#request');
ApiController.php
class ApiController extends Controller
{
public function request($controller, $action)
{
//some error check
$controller = 'App\Http\Controllers\\' . ucfirst($controller) . 'Controller';
$params = "Put some params here";
$reflectionMethod = new \ReflectionMethod($controller, $action);
$reflectionMethod->invoke(new $controller, $params);
}
}
ContentController.php
class ContentController extends Controller
{
public function show($params)
{
dd($params);
}
}
And it is working!
Thx a lot!
A better option is to use App::call(); invoking controller with ReflectionMethod might not let you use response() inside your new forwarded controller and other laravel goodies.
Here is my try on this: /api/request/content/show
Routes web.php or api.php
use App\Http\Controllers\ApiController;
Route::get('api/request/{controller}/{action}', [ApiController::class, 'request']);
ApiController.php
class ApiController extends Controller
{
public function request($controller, $action)
{
//some error check
return App::call('App\Http\Controllers\\'.ucfirst($controller).'Controller#'.$action);
}
}
ContentController.php
use Illuminate\Http\Request;
class ContentController extends Controller
{
public function show(Request $request)
{
dd($request);
}
}
This will allow you more freedom.

Call a method from one controller inside another

Is it possible to call a method from one controller inside another controller in Laravel 5 (regardless the http method used to access each method)?
This is how I have done it. Use the use keyword to make the OtherController available. Then you can call a method from that class on instantiation.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\OtherController;
class MyController extends Controller {
public function __construct()
{
//Calling a method that is from the OtherController
$result = (new OtherController)->method();
}
}
Also check out the concept of a Command in Laravel. It might give you more flexibility than the method above.
use App\Http\Controllers\TargetsController;
// this controller contains a function to call
class OrganizationController extends Controller {
public function createHolidays() {
// first create the reference of this controller
$b = new TargetsController();
$mob = 9898989898;
$msg = "i am ready to send a msg";
// parameter will be same
$result = $b->mytesting($msg, $mob);
log::info('my testing function call with return value' . $result);
}
}
// this controller calls it
class TargetsController extends Controller {
public function mytesting($msg, $mob) {
log::info('my testing function call');
log::info('my mob:-' . $mob . 'my msg:-' . $msg);
$a = 10;
return $a;
}
}

How to call custom controller from index controller in Zend Framework

I am newbie in Zend framework.And i have made sample project in netbeans.And it is working properly displaying index.phtml .But , I need to call my controller.What I have tried is below.
IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
firstExample::indexAction();
}
}
And I have deleted all the content of index.phtml(just a blank file) , coz i don't want to render this view.
My custom controller is:
firstExampleController.php
<?php
class firstExample extends Zend_Controller_Action{
public function indexAction(){
self::sum();
}
public function sum(){
$this->view->x=2;
$this->view->y=4;
$this->view->sum=x + y;
}
}
?>
firstExample.phtml
<?php
echo 'hi';
echo $this->view->sum();
?>
How to display sum method in firstExample.php.
It just shows blank page after hitting below URL.
http://localhost/zendWithNetbeans/public/
I think after hitting on above URL , execution first goes to index.php in public folder.And I didn't change the content of index.php
You are using controller (MVC) incorrectly, Controller should not do any business logic, in your case sum method. Controller only responsible controlling request and glueing model and view together. That's why you have problems now calling it.
Create Model add method sum, and use in any controller you want. From controller you may pass model to view.
Here is example: http://framework.zend.com/manual/en/learning.quickstart.create-model.html it uses database, but it's not necessary to use with database.
Basically your sum example could look like:
class Application_Sum_Model {
public function sum($x, $y) {
return ($x + $y);
}
}
class IndexContoler extends Zend_Controller_Action {
public function someAction() {
$model = new Application_Sum_Model();
//passing data to view that's okay
$this->view->y = $y;
$this->view->x = $x;
$this->view->sum = $model->sum($x, $y); //business logic on mode
}
}
Please read how controller is working, http://framework.zend.com/manual/en/zend.controller.quickstart.html

Code Igniter controller - can't use uri->segment with function index()?

I'm working with code igniter and for some reason, the url http://mysite.com/account/100 gives me a 404 error but http://mysite.com/account actualy works. Here's what my controller account.php looks like.
class account extends Controller {
function account()
{
parent::Controller();
}
function index()
{
echo 'hello';
echo $this->uri->segment(2);
}
}
Any idea what's wrong?
I just tested a simple account class like you have and it is trying to call 100 as a method of account, instead your URL after index.php should be account/index/100 and it works fine.
This can be solved using routing for example.
$route['account/(:num)'] = "accounts/index/$1";
is what you are looking for. You can look over the URI Routing user guide
for more info.
CodeIgniter requires that your controller name be capitalized, and the filename lowercase so try changing your controller to.
class Account extends Controller { }
and your filename account.php
Add this route and you are good
$route['account/(:any)'] = "account/index/$1";
This is not working because when you request http://example.com/account, it looks index() method.
But when you request http://example.com/account/100, is looking for 100() method. Which is not present.
You may tweak the code like this.
class account extends Controller {
public function account()
{
parent::Controller();
}
public function index()
{
echo 'hello';
}
public function id(){
echo $this->uri->segment(2);
}
public function alternative($id){
echo $id;
}
}
you'll call url like this: http://example.com/account/id/100
or you can do like this
http://example.com/account/alternative/100

Resources