Redirecting to own controller methods - laravel

Signup controller:
<?php
class Signup extends BaseController
{
public function getNew()
{
return Redirect::action('Signup#success');
}
public function success()
{
echo "successful";
}
}
?>
Routes.php
Route::controller('signup', 'Signup');
So when I go to localhost/signup/new it should throw out successful but ends with Unknown action [Signup#success]. error.
I read many same topics before but didn't help me at this case.

you need to use a certain format.. see RESTful Controllers
it says "Next, just add methods to your controller, prefixed with the HTTP verb they respond to"
change your Signup controller to:
<?php
class Signup extends BaseController
{
public function getNew()
{
return Redirect::action('Signup#getSuccess');
}
public function getSuccess()
{
echo "successful";
}
}
?>
and everything should work now..

Related

URL change issue after form post in Codeigniter

how can i manage url in address bar after posting a form or after loading a page after submission.
Is it possible to manage with routing ?
<?php
public function index(){
$this->load->view('login');
}
public function login_process(){
....... code......
if($login==true){
$this->load->view('dashboard'); // Url is not changing but view is loaded
}else{
$this->load->view('login');
}
}
?>
Hope this will help you :
Use redirect() method from url helper , make sure you load it in controller or in autoload.php
public function login_process()
{
....... code......
if($login === TRUE)
{
redirect('controller_name/dashboard','refresh');
/*$this->load->view('dashboard'); */
}
else
{
redirect('controller_name/index','refresh');
/*$this->load->view('login');*/
}
}
For more :https://www.codeigniter.com/user_guide/helpers/url_helper.html#redirect
You should create another method called dashboard and you should redirect to it like following
class Example_Controller extends CI_Controller {
public function index(){
$this->load->view('login');
}
public function dashboard(){
$this->load->view('dashboard');
}
public function login_process(){
// Your Code
redirect('Example_Controller/' . (($login==true) ? 'dashboard' : 'index'));
}
}
replace Example_Controller with your controller name.
and add following lines in routes.php
$route['login'] = 'Example_Controller/index';
$route['dashboard'] = 'Example_Controller/dashboard';

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.

Laravel constructor redirect is not working?

I have a controller with several methods and I need to add a specific authorization check. If authorization failed then redirect login page. So for this reason i have created one private function and this function call in constructor.
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('web');
$this->isLogin();
}
private function isLogin()
{
if (!empty(Auth::user())) {
echo "Hello";
} else {
echo "Fasd";
return Redirect::to('/login');
}
}
}
If auth is not found is does not redirect to login. What i write extra code for this?
Doing login page redirect use redirect::route into login page alias name routes.php.
public function isLogin()
{
if (!empty(Auth::user())) {
echo "Hello";
} else {
echo "Fasd";
return Redirect::route('login');
}
}

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