It's so weird. When a user is logged out of my application, press back to the previous page, which is user/login I get this error:
{"error":
{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException",
"message":"Call to a member function with()
on a non-object","file":"\/home\/nicola\/projects\/pensions\/app\/controllers\/UserController.php",
"line":79}}
The page isn't being reloaded though. I put a die() in the application and it doesn't hit it, so the error must be being generated from the cached version. However when browsed to directly, there is no problem accessing user/login. It's only when pressing back.
line 72 is:
$this->layout->with($this->data);
Which basically means it is saying layout isn't an object.
Layout is made into a view object in the base controller: ` $this->layout = \View::make($this->layout);
EDIT `
//the function causing the error
public function getLogin ()
{
$this->data['page_title'] = 'user/login';
$this->layout->with($this->data);//line 72
$this->layout->content = \View::make('user.login', $this->data);
}
//function that makes the $layout an object in base controller
protected function setupLayout ()
{
if (!is_null($this->layout) && !\Request::ajax()) {
$this->layout = \View::make($this->layout);
}
}
Related
I have a "routing" problem on CodeIgniter 4.2.7.
My home page is displayed well, the page also reloads well when I click on "home" in my menu, on this side there is no problem, however when I want to access another page I have the message error following "Error 404".
Here is my controller:
class c_accueil extends BaseController
{
public function index()
{
$data['titre'] = "Accueil";
return
view('v_menu')
. view('v_accueil',$data)
. view('v_footer');
}
public function espaceNintendo()
{
$data['titre'] = "Espace Nintendo";
return
view('v_menu')
. view('v_espaceNintendo', $data)
. view('v_footer');
}
Here is my base url:
public $baseURL = 'http://localhost:63342/ProjetWeb';
Here is my route:
$routes->get('/', 'c_accueil::index');$routes->get('/public/espaceNintendo', 'c_accueil::espaceNintendo');
Here is my view (v_menu) :
<?=anchor(base_url().'/public/', 'Accueil')?>
<?=anchor(base_url().'/public/espaceNintendo', 'Espace Nintendo')?>
So "Accueil" works fine but not "espaceNintendo"
Url of my home page which works well:
http://localhost:63342/ProjetWeb/public/
However, the URL as soon as I click on "Espace nintendo" is this:
http://localhost:63342/ProjetWeb/public/espaceNintendo
But it gives me "error 404".
Do you have any idea where the problem comes from?
My helpers are well loaded on my base_controller:
protected $helpers = ['html', 'form', 'url'];
I asked a question about this earlier but I'm still stuck and having some issues.
Right now, if I go to site.com/status/12345 then I will get my status as I would expect. The route with the id (12345) hits a controller method which passes that id in and returns the json version of it in the view, where I'm using it as a prop and calling a function with it on page load.
However, if I just go to site.com/status (which should hit my index method, returning only the base view) then I get 'undefined variable entryKey'.
Obviously, I'm adding my entryKey value from the controller into my blade to be passed in as a prop, which seems to be working if there is a URL segment for it, but now I can't just get my base view to return with site.com/status
How can I fix this so that site.com/status hits my index method and site.com/status/12345 hits the detail method and passes the id in as a prop?
web.php (routes)
Route::get('/entry/status', 'entry\entryController#index')
->name('entry.status');
Route::get('/entry/status/{id}', 'entry\entryController#details')
->name('entry.status');
controller.php
public function index() {
return view('entry.status');
}
public function details($id) {
if (request()->wantsJson()) {
return json_encode(entry::getDetails($id));
}
$entryID = json_encode($id);
return view('entry.status')
->with('entryKey', $entryID);
}
status.blade.php
<entry-dashboard :entry-key="{{$entryKey}}"></entry-dashboard>
detail.vue
props: [
'entryKey'
],
created() {
this.setDetailWindow(this.entryKey);
},
setDetailWindow(event) {
this.detailId = event.taskt_id;
axios.get('/entry/status/' + this.detailId)
.then(response => {
...
...
}
After spending so many days, am trying to get some help from experts.
I am stuck with login redirection in my yii2 application only in chrome browser,
This is my controller class,
class InvitationsController extends Controller
{
public function beforeAction($action)
{ $array=array('index','imageupload','template','category','subcategory','slug','chooseanotherdesign');
if(!in_array($action->id, $array))
{
if (\Yii::$app->getUser()->isGuest &&
\Yii::$app->getRequest()->url !== Url::to(\Yii::$app->getUser()->loginUrl)
) {
\Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->loginUrl,FALSE);
}
}
return parent::beforeAction($action);
}
public function actionGenerateevent(){
$redirectUrl="";
if(Yii::$app->request->post()){
unset(Yii::$app->session['copyinvitation']);
unset(Yii::$app->session['eventform']);
Yii::$app->session['eventform']=Yii::$app->request->post();
}
if (!Yii::$app->user->isGuest)
{
$eventid=$this->invitation->savecontinue(Yii::$app->session['eventform']);
$eventdata=$this->invitation->getEventById($eventid);
$refurl=Yii::$app->session['eventform']['refererurl'];
$aa['Events']=$eventdata;
$aa['refererurl']=$refurl;
Yii::$app->session['eventform']=$aa;
$redirectUrl = Yii::$app->urlManager->createAbsoluteUrl(['invitations/event/'.$eventdata['event_token']]);
return $this->redirect($redirectUrl);
}
}
}
My workflow
step1: submitting formdata to controller xx-action
step2: If user login it will proceed further action
Else
am trying to store the values in session then redirecting the page to login
step 3: after successful login am return back to same xx-action
This workflow is working fine in firefox but chrome it's making infinitive loop its not going through the login page.
Please refer am attached the screenshot
Please help me to solve this issue.
I can't infere how are you calling your actionGenerateevent() but you seems to have an error there:
$redirectUrl=""; //empty
...
return $this->redirect($redirectUrl); //still empty
Since you are not setting your $redirectUrl, your redirect is redirecting you to the current (same) url again and again, causing the loop.
This is the function used by redirectUrl() method: Url::to(). Its docs says:
an empty string: the currently requested URL will be returned;
I have this controller: Resales, and I am in the Administrator controller and need to create a new Resale.. how should I do that?
first option:
Administrator form calls /resales/addResales
second option: Administrator has a method addResale that loads the Resale model and inserts it.
What should I do?
thanks
Administrator controller shouldn't be loading the Resales controller - that should be a model that both controllers use. You should load your Resales model into the Administrator controller in the $uses = array() property at the top of your controller file:
class AdministratorController extends AppController {
var $uses = array('Resale', //the rest of your models);
public function createResale() {
$this->Resale->create();
$this->Resale->set($this->data['Resale']);
$this->Resale->save();
}
}
Other options are that you could use Ajax to post the request for you, or you could use $this->requestAction() in your Administrator controller to use a processing function in your Resales controller:
// administrator controller
public function createResale() {
// define your data here
$result = $this->requestAction('resales/create', array($data_array));
}
Have a look at the manual for more information on requestAction:
http://book.cakephp.org/2.0/en/controllers.html
EDIT
You've just asked about views. In this case, there's no real need to create a view for createResale(), what you might want to do instead is set a Session flash message, then redirect back to your form. You will need to ensure you've included the Session helper at the top of your controller:
class AdministratorController extends AppController {
var $helpers = array('Session', // any others here);
Then you prevent the layout and render of views, do your thing and set a session flash with the results message:
public function createResale() {
// don't render a view or layout
$this->layout = '';
$this->render(false);
// process your request
$result = // do stuff... return true or false for result
$msg = $result ? 'Added successfully!' : 'Error adding resale!';
// set flash message
$this->Session->setFlash($msg);
// return to that form
$this->redirect(array('action' => 'formYouCameFrom'));
}
Now on your form, you'll simply do this:
echo $this->Session->flash();
... which will output the results.
How do I change the content for a user when he logs in? I mean like enabling voting, changing "login" to "logout" etc.
What I think to do is to start the session when user logs in (I am preferring to start session only when user logs in, not all the time). Then add data to the session's cookie like-
//controller
$moredata = array(
'username' => $this->username,
'login' => TRUE
);
$this->session->set_userdata($modedata);
//redirect
Then in the other controller, where he has been redirected I check the following-
$login = $this->session->userdata('login');
if ($login==TRUE)
Depending on the 'if' condition I will pass a variable to the view, with the help of that variable I will forward only the div/sections which should be shown to a logged-in user.
The problem is, while performing the above comparison Codeigniter shows following error (remember I haven't added 'session' in autoload array yet)
Message: Undefined property: NameOfController::$session
And If I set following in the autoload file
$autoload['libraries'] = array('session');
then the "if ($login==TRUE)" comparison always shows FALSE.
What should I do?
If I were you, I'd place all your session checks in a base controller which all your other main controllers extend. This allows you to keep things DRY:
class BaseController extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function isLoggedIn()
{
// Will return true or false
return $this->session->userdata('login');
}
}
And in one of your functional controllers (the example below handles users):
class UserController extends BaseController {
public function __construct()
{
parent::__construct();
}
public function profile()
{
// Redirect if not logged in
if (!$this->isLoggedIn()) {
$this->redirect('some/other/page')
}
}
public function register()
{
// Show different HTML if not logged in
$data = array(
'isLoggedIn' => $this->isLoggedIn()
);
$this->load->view('register', $data);
}
}
The second method in UserController allows you to render different content in your view:
<? if ($isLoggedIn): ?>
<p>You're logged in!</p>
<? else: ?>
<p>Not logged in</p>
<? endif; ?>
On my last project we created a simple permissions helper that had functions to check for logged-in status and for privilege levels. Then we'd just call the helper's functions as needed from anywhere in the system. If the user is logged in and has privs for that content then they get the content - otherwise we'd redirect them to a registration or other error page. Since all of that logic is in the helper functions, we could wrap any permission-requiring code in a quick permissions call like if(is_logged_in()){code requiring login to access}.