CakePHP 4 - view helper - error reading session values - session

I am still fairly new with CakePHP and leaning with version 4. I can read PHP session values in a template index.php file with below. I think that is wrong way to do but I tested anyway.
$userrole_id = $this->request->getSession()->read('Auth.userrole_id');
And now I am trying to read session values in View Helper so that I can use same code in many different places. But above code throws an error with "Call to a member function getSession() on null". And as you can see in the above one line code, I have Authentication set up and that is working fine. And I have helper loaded in AppView.php, and that is loaded fine, too.
I am pretty sure I am missing basic stuff but I cannot figure it out. Any suggestions would be appreciated.

I found a solution in the official documentation, and I needed to do like below:
$this->getView()->getRequest()->getSession()->read('Auth.userrole_id');

Related

Where does the output of dd() and dump() get written when developing Laravel in VS Code?

I'm developing a Laravel 8.x app in the current version of VS Code. I have a dump() statement in a Controller that I've written so that I can determine precisely what's in the $request that is being handled because I suspect that my date isn't exactly what I think it is. I've added this to the store() method: $request->dump() and I'm pretty sure it has executed but I can't find the output from the dump() function anywhere. I've got Debugbar installed and it does not show the dump() output. Ditto for any window of the browser like the console, inspector, etc. I've looked in the laravel.log but it's not there either. I've checked each of my terminal windows - the ones dedicated to php artisan serve and npm run watch and the one I use for all other commands but I can't find anything there. I'm out of ideas on where to look.
If the Laravel docs tell you where to find the output, I missed it; they describe what dump() and dd() do but I can't find where they describe where the output is written.
I'm pretty new to Laravel and I'm working alone so I have no work colleagues to ask. Could someone kindly tell me where to find the output of dump() so that I can debug this program?
None of the comments pointed me to precisely where I eventually found the dd() output so I'm posting it here for the sake of any future person who has the same issue. I finally found it in the browser, in the console tab (I'm using Firefox) and within the Response to the Axios statement that tried to write the new record to the database. Here's a picture:
I had assumed that the information wouldn't require me to drill down as far as I did; I thought it would be as visible as normal console.log() output.
I'm very grateful to those who replied to my question; you finally got me looking in the exact right place. I've never used dd() or dump() before, even in vanilla PHP, so I really had no idea where the output got written.

Trouble with Codeigniter Cron Job

I am haveing trouble with getting a cron job to work with codeigniter
I have used these instructions.
https://www.codeigniter.com/user_guide/general/cli.html
But it is not working
Here is what I have.
php /home4/markwolf/public_html/propalert.asia/index.php admin_notifications index
But it is not finding the right controller or the method, instead it is going to the "default_controler". Everything else I've tried gets a 404 not found error.
I've been pulling my hair out for two days trying to get this to work so any help would be greatly appreciated.
Thanks
EDIT
This works:
wget -q http://propalert.asia/admin_notifications
But the problem with this method is that Codeigniter's method for protecting the script by checking whether it is accessed by cli returns false.
I came across this post at CI's forum describing the same problem.
https://ellislab.com/forums/viewthread/236475/#1055830
Unfortunately he never got an answer there either.
I finally found the solution:
/usr/bin/php-cli -q /home4/markwolf/public_html/propalert.asia/index.php admin_notifications index
the key is "php-cli" instead of "php".
But then there is a bug in the CI core that needs to be fixed. You can find the solution to that here:
https://ellislab.com/forums/viewthread/227672/

codeigniter session data gets lost

i already posted this question but still wasnt able to resolve this issue.
seems that everyone has this problem with codeigniter .
When i set my session in a controller it works perfectly and i can display it.
WHen i move to another controller, the (CUSTOM) session data is completely lost.
i tried changing my cookie_domain in config.php. Since i am on localhost i tried localhost with without / and localhost/codeigniter and sodeigniter all did not work. i am lost
btw, i read somewhere that this happens when 2 ajax requests happen at the same time. could that be the problem?
Or maybe tell me how you resolved the problem if you had same issues
screw this, i am swithing to php native sessions. if anyone wants to do that,
http://codeigniter.com/wiki/PHPSession
Check and double-check your code or any external libraries you are using for a stray sess_destroy(). I ran into a similar problem where I was storing an id in the session for reference but if the user then logs in the SimpleLogin library I was using for logins just destroys the entire session including data I did not want to lose.
This is what I get for not writing my own code.

Codeigniter 1.7.2: function image() seems to no longer exist, what is the replacement?

A society I belong to have a website which someone who left a few years ago set up. It was done using codeigniter (which was probably rather excessive given that the website is pretty simple.)
The server it is hosted on updated Php recently, which resulted in the website completely breaking. It now simply returns a page full of deprecated function error messages like
"A PHP Error was encountered
Severity: 8192
Message: Function set_magic_quotes_runtime() is deprecated
Filename: codeigniter/CodeIgniter.php
Line Number: 60
"
Reading the CodeIgniter documentation it seemed the new version would work fine with the latest PhP installed on the server. I installed a the latest version of CodeIgniter (1.7.2) and copied over the views and database settings. I now get an error message about the image() function being undefined:
"Fatal error: Call to undefined function image() in /public_html/newSystem/application/views/welcome_index.php on line 32"
I tried searching through all the CodeIgniter documentation, and couldn't find any reference to this function.
it is used in the following fasion:
<?=image('welcome_index_splash-text.gif')?>
which led me to believe it was related to the img() function which converts text into an element, but replacing image() with img() gave the same error message. (Well, with img being undefined instead of image)
Since I have never used CodeIgniter before, there is probably a very simple fix, but it doesn't seem readily available from the UsersGuide.
The lesson would be not to use a framework like CodeIgniter when static HTML would have done, but it wasn't my decision to set it up like this in the first place!
Thank you for any help.
Codeigniter has a helper function called img() which returns the HTML code for the image provided as argument. If you get that error you're probably not including the right helper library like this:
$this->load->helper('html');
in your controller.
Reference: http://codeigniter.com/user_guide/helpers/html_helper.html

codeigniter modular extensions

I'm trying to implement modular extensions into a codeigniter 2 setup but am having a few problems. I followed the installation instructions on the wiki and everything was working fine. Then I started to play around a bit and try and use it. All I did was create a new module called users with the required folders and added a model class called users_m. I then tried to load this from my welcome module controller. According to the wiki this should be very straightforward. I simply added this line
$this->load->model('users/users_m');
to the constructor of my welcome controller.
Unfortunately at this point I get the white screen of death.
So I tried something else. This time I removed the load model line and added
$this->output->enable_profiler(TRUE);
This time I got the welcome page displayed and I got the profiler, but at the top of the page I got this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI::$profiler
Filename: MX/Loader.php
Line Number: 145
I don't know if these two tries are related or not, but there's obviously something not right with my setup. Could someone point me in the right direction?
If you accessing the model from the controller in the same module, you can load it using just:
$this->load->model(‘user_m’);
You only have to do $this->load->model(‘module/model_name’); when your cross loading between modules.
Just to make sure, your model is located here right?
application/modules/users/models/users_m.php
As for the profiler error:
1) Have you done installation step 5 and put the Modular Extensions core files into application/core?
2) Do you have the latest version of HMVC? There have been updates to mx/loader.php in the last couple days.
Ps. great tutorial on HMVC: http://net.tutsplus.com/tutorials/php/hvmc-an-introduction-and-application/
if you have folder structure like:
application/
modules/
users/
models/
users_m.php
then use this to call modelsin you controller
$this->load->model('users/users_m','',TRUE);

Resources