After ssl implementation in cakephp 3 return $this->redirect() stop working - cakephp-3.0

I have implemented the below code for ssl implementation, but after that my redirect is no longer working:
Here is my code:
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
class WidgetsController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Security', ['blackHoleCallback' => 'forceSSL']);
}
public function beforeFilter(Event $event)
{
$this->Security->requireSecure();
}
public function testRedirect()
{
**$this->redirect("/controller/Somewhere");**
}
public function forceSSL()
{
return $this->redirect('https://' . env('SERVER_NAME') . $this->request->getRequestTarget());
}
}
Any help is appreciated.

Related

Yii2 access class from within the class

I'm trying to use the findOn from within the class that I want to search. Is this possible or is there a better way?
class CmsSettings extends ActiveRecord
{
public static function tableName()
{
return 'cms_settings';
}
//not working
public static function run(){
$results = CmsSettings::findOn(1):
return $results;
}
// not working
public static function check(){
$results = CmsSettings::findOn(1):
if($results->somesetting){
return true;
}
}
}
You should probably use static::findOne(1). By using self or CmsSettings you are just hardcoding returned type, which makes this class less flexible and will give you unexpected results on inheritance. For example if you create child model which extends your class:
class CmsSettings extends ActiveRecord {
public static function run() {
$results = CmsSettings::findOne(1);
return $results;
}
// ...
}
class ChildCmsSettings extends CmsSettings {
}
You expect that ChildCmsSettings::run() will return instance of ChildCmsSettings. Wrong - you will get CmsSettings. But if you write this method with using static:
public static function run() {
$results = static::findOne(1);
return $results;
}
You will get instance of class which you're used for call run() - ChildCmsSettings.
Use self
Refer findOne()
class CmsSettings extends ActiveRecord
{
public static function tableName()
{
return 'cms_settings';
}
public static function run()
{
$results = self::findOne(1);
return $results;
}
public static function check()
{
$results = self::findOne(1);
if ($results->somesetting) {
return true;
}
}
}

how to get another function in another controller (Codeigniter)

i made controller name crud.php this the code :
class Crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_data');
$this->load->helper('url');
}
public function admin(){
$data['produk'] = $this->m_data->tampil_data()->result();
$this->load->view('v_admin',$data);
}
}
i want take the admin() to place in admin.php code here :
class Admin extends CI_Controller{
function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
function index(){
$this->crud->admin();
}
}
what's code for take another function in another controller.. that's possible or not?
You could try to require Crud controller within Admin controller :
public function index()
{
require_once('crud.php');
$crud = new crud();
$crud->admin();
}
I do not know if I understood your question very well, but you could have the controller Admin extend the Crud controller, so he would have access to the admin ()
class Admin extends Crud{
function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
function index(){
$this->admin();
}
}
Loading another controller inside a controller is a bad idea. It can lead to hard to find bugs. One solution is to make Crud a custom library that the controller loads.
File: /application/libraries/Crud.php
class Crud
{
protected $CI;
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->model('m_data');
$this->CI->load->helper('url');
}
public function admin()
{
$data['produk'] = $this->CI->m_data->tampil_data()->result();
$this->CI->load->view('v_admin', $data);
}
}
Then the controller loads and uses the custom class.
class Admin extends CI_Controller{
public function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$this->load->library('crud');
$this->crud->admin();
}
}
You could load crud in the constructor too. That would make sense if other methods in the controller use the library.
May it can be useful for you, if you will customize your code in the view:
VIEW
your-link-to-another-controller

Why I can't use BlocksHelper from custom plugin?

I create "BlocksHelper" in my custom plugin, but I can't use it without fatal errors. What is wrong?
plugin/myPlugin/src/View/Helper/BlocksHelper.php:
namespace myPlugin\View\Helper;
use Cake\View\Helper;
class BlockHelper extends Helper{
}
bootstrap.php:
...
Plugin::load(myPlugin, ['autoload' => true]);
View/AppView.php:
class AppView extends View
{
public function initialize()
{
parent::initialize();
$this->loadHelper('myPlugin.Blocks');
...
}
}

How savind data from model?

I don`t understand how savind data from model (like User.php)
When i run saveData() from Controller new row not creared.
class Users extends \yii\db\ActiveRecord
{
....
public function saveData() {
$this->name = 'test_user';
$this->save();
}
}
I don`t want save data from controller.
What i do wrong?
Check if there are validation errors:
class Users extends \yii\db\ActiveRecord
{
....
public function saveData() {
$this->name = 'test_user';
if($this->save() == false)
{
var_dump($this->errors);
}
}
}

Model loaded in Codeigniter controller's constructor is not available by other functions of same controllers

My model: as seen below, very basic
class User extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getAll()
{
$this->db->order_by("lastName", "asc");
$this->db->order_by("firstName", "asc");
$this->db->order_by("userName", "asc");
$query = $this->db->get('user');
// test for result
if($query->num_rows() > 0)
{
return $query->result();
}
return NULL;
}
}
My controller: actually part of my controller, every time loading the users/display function by default route, the error (further down) shows up. Should a model loaded in a controller's contructor be available for all other function in the same controller?
class Users extends CI_Controller
{
function __contruct()
{
parent::__construct();
$this->load->model('user');
}
function display()
{
$data['users'] = $this->user->getAll();
$head['pageTitle'] = 'Users Panel';
$this->load->view('security/redirect');
$this->load->view('template/head', $head);
$this->load->view('user/usersPanel', $data);
$this->load->view('template/foot');
}
}
My error: refering to the line, "$data['users'] = $this->user->getAll()", in above controller
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$user
My environment:
Codeigniter 2.1.0;
Mac Lion;
MAMP 2.0;
Shouldn't this:
class Users extends CI_Controller
{
function __contruct()
{
be like this:
class Users extends CI_Controller
{
function __construct()
{
replace contruct with construct.
Shouldn't this:
$data['users'] = $this->user->getAll();
be this:
$data['users'] = $this->user_model->getAll();
sorry
also the model name:
$this->load->model('user_model');
and class name User_model extends CI_Model
All of my CI projects are set up this way.
http://codeigniter.com/user_guide/general/models.html