I am having some problems with redirection on Phalcon 0.8b.
Here is my code:
<?php
class UsersController extends \Phalcon\Mvc\Controller {
public function loginAction() {
if($this->session->get('user')) {
$this->response->redirect('users/view/');
exit;
}
}
}
Basically, the code checks if the user is logged in, and then redirects them. I am not getting a redirection to happen, however. It just white screens on me. Am I doing something wrong? The documentation hasn't been very helpful.
Thank you!
The 'exit' is avoiding that the response headers be sent to client, the following must work:
<?php
class UsersController extends \Phalcon\Mvc\Controller {
public function loginAction() {
if($this->session->get('user')) {
return $this->response->redirect('users/view/');
}
}
}
Related
My normal web app runs w/o any issue. Then I wanted to experiment with APIs. I enabled Passport since I need api authorization (but at this moment, I rather want to get this thing working and I have no idea whether it is a problem with Passport) and I wanted to get simple json output of specific Product. So far, I was not able to get it working. I'll describe contents of each file and if someone can direct me to find the issue in my code, that would be great.
Resources\Product.php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class Product extends JsonResource
{
public function toArray($request)
{
return parent::toArray($request);
}
}
Providers\AuthServiceProviders.php
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
User.php
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
...
}
ProductController.php
class ProductController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function apiShow(Product $product)
{
return new ProductResource($product);
}
...
}
routes/api.php
Route::get('/products/{product}', 'ProductController#apiShow');
Now if I go to http://localhost/public/products/1, it displays the page as expected. But if I type in http://localhost/public/api/products/1, it will always go to home page which is set to localhost/public in HomeController.
If I modify routes/api.php as:
Route::get('/products/{id}', function($id) {
return Product::find($id);
});
I get the correct json output in the browser.
I have create a Test controller using the code in : Create Custom page in Prestashop 1.5.3.1
class MyPageController extends FrontController
{
public $php_self = 'mypage';
public function init() {
parent::init();
}
public function initContent() {
parent::initContent();
die('test!');
}
}
I put it in the /controllers/front/MyPageController.php and it does not work. i get a clean html page.
What am I missing ?
link to custom page : http://www.funtoy.co.il/index.php?controller=MyPage
Edit 1 :
fix the semicolon at $php_self = 'mypage';
Thanks.
yaniv abo
Remove cache/class_index.php to clear the cache.
Trying to figure out why something like the below cant work. There is a tab in the preferences admin section that points to this controller but when going to it it always output a blank page. I have tried several things but the below is basically what I need. There is no MODEL... I simply need this to get the uploaded file for postProcessing...
The controller file...
class AdminAstroImporterController extends ModuleAdminController {
public function initContent() {
parent::initContent();
return $this->display(__FILE__, 'import.tpl');
}
public function postProcess() {
//do something here
}
}
Looks like you can overide the actual content output by doing as shown in the initContent() function shown below. The 'content' in the smarty assign can be any html you generate yourself.
class AstroImporterAdminController extends AdminController {
public function __construct() {
parent::__construct();
//load current settings
$this->data = unserialize(Configuration::get('ASTRO_IMPORTER'));
}
public function initContent() {
parent::initContent();
$this->show_toolbar = false;
$this->context->smarty->assign(array(
'content' => $this->renderSettings().$this->renderForm().$this->displayFields(),
));
}
I found that this works:
public function renderList()
{
global $currentIndex, $cookie;
$smarty = $this->context->smarty;
$smarty->assign('currentIndex', $currentIndex);
return $this->context->smarty->fetch($this->getTemplatePath().'/main.tpl');
}
Although its dirty as hell, it seems cleaner than Amb3rL4nn answer.
In v1.4 it was very easy to create a tab (and easy to find documentation) I wonder why they changed it and didn't supply an docs.
I want to redirect to another pages after successful registration. How I can I restrict this pages to be visited directly from url. only display after registration.
public function actionRegistration()
{
.......
if($model->save())
{
$this->redirect(Yii::app()->request->baseUrl.'/site/success_registration');
}
............
}
public function actionSuccess_registration()
{
$this->render('success_registration');
}
You could probably add some rules and filters, or you could check the referrer in the actionSuccess_registration but the simplest way to make the view non-accessible from anywhere else would be to render the success_registration view from inside actionRegistration. actionSuccess_registration is therefore redundant if all it does is render the view.
public function actionRegistration()
{
.......
if($model->save())
{
$this->render('success_registration');
return;
}
............
}
check this out for authentication in yii.
so u could do like this:
class YourController extends CController
{
......
public function filters()
{
return array(
'accessControl',
);
}
public function accessRules()
{
return array(
array('deny',
'actions'=>array('registration'),
'users'=>array('?'),
),
array('allow',
'actions'=>array('success_registration'),
'users'=>array('#'),
),
array('deny',
'actions'=>array('delete'),
'users'=>array('*'),
),
);
}
}
in your method
public function actionRegistration()
{
.......
if($model->save())
{
$this->redirect($this->createUrl('site/success_registration'));
}
............
}
It would be easier if you used flash messages instead. That's what they are there for.
public function actionRegister()
{
if(Yii::app()->user->hasFlash('registered')) {
$this->render('success_login');
} else {
// Process POST registration data here.
// If registration was successul you do:
Yii::app()->user->setFlash('registered',true);
$this->refresh();
// Otherwhise you render the registration form here
}
}
you should go to the project file and go to the protected folder ->controller folder and then open the SiteController.php
please find the "public action actionLogin()" in the SiteController.php.
after that find this line:
$this->redirect(Yii::app()->user->returnUrl);
in the actionLogin() function.
for example i want it is redirect to the user controller.i add .'?r=user' at the end of code.
$this->redirect(Yii::app()->user->returnUrl.'?r=user');
I am new on Zend Framwork(MVC). I want to crate simple a Form with some HTML control.
I have create one controller IndexController, code are as follows:
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
/*$this->view->var = 'User Login Page';*/
$form = new Form_Login();
$this->view->form=$form;
}
}
And my Form's code in application/forms/Login.php:
<?php
require_once('Zend/Form.php');
class Form_Login extends Zend_Form
{
public function init()
{
parent::__construct($options);
// the code bellow will create element
$username = $this->CreateElement('text','username');
$username->setLabel("Username:");
// and
$submit= $this->CreateElement("submit","submit");
$submit->setLabel("Submit");
// now add elements to the form as
$this->addElements(array(
$username,
$submit
));
}
}
?>
When i run this project then its show an error like this:
**Fatal error: Class 'Form_Login' not found in C:\xampp\htdocs\LoginForm\application\controllers\IndexController.php on line 16**
Please help me...
Thanks
Pankaj
Everything looks good, make sure your Login.php file has this as the first lines:
<?php
class Form_Login extends Zend_Form
{
public function init()
If that doesn't help, you might want to check your index.php/Bootstrap.php files and server configuration to make sure all paths are correct.