Session is not read in multiple controllers in cakephp - session

I'm using Sessions in a cakephp app but it looks like the session that I've set up is not shared between the various controllers that I'm using. So lets say I have
PagesController
public $components = array( 'Email', 'Session', 'RequestHandler', 'Cookie');
//this is pages/home
public function home(){
$this->Session->write("bunny", "123456");
debug($this->Session->read("bunny"));
}
PersonController
public $components = array( 'Email', 'Session', 'RequestHandler', 'Cookie');
//this is person/index
public function index(){
debug($this->Session->read("bunny");
}
When I go to the url http://domian.org/person/index, that debug line is null. Shouldnt it print out "123456"?

I cannot say it bug but as far as i have worked on the cake php. I found that most of the people are facing the same problem use php default function here.
Use session_start(); before filter function or use ob_clean(); before filter function i think this may resolve your issue

Related

Laravel Livewire The POST method is not supported for this route

As soon as I inserted this title the system showed me all the similar questions, and none of them help me. I get this error "The POST method is not supported for this route." no matter what I try. Even worse, is I already made another component with the identical logic, and that one works good.
here are the routes: (teeoffform works, bulletin does not)
Route::get('/bulletin', function () {
return view('bulletin');
});
Route::get('/teeoffform', function () {
return view('teeoffform');
});
Here are the form tags: both identical one works one doesn't
<form wire:submit.prevent="submit" method="POST">
this is my component from the one that doesn't work (bulletin)
the only difference from the other one that does work, is that there is no rendering method, so I tried to take it out and see if that was the problem, but no luck... I thought, since my route is alrady calling a view maybe the conflict is there... but it doesn't matter, I get the error anyways, and I'm out of ideas.
<?php
namespace App\Http\Livewire;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use App\Models\Bulletins;
use App\Models\User;
class Bulletin extends Component
{
public $title;
public $message;
public $messagesending;
public $user_email;
public $userTable_email;
public $expires;
public $success_message;
protected $rules = [
'title' => 'required',
'message' => 'required',
'user_email' => 'required',
'expires' => 'required',
];
public function render()
{
return view('livewire.bulletin', ['email_data' => User::orderBy('email','asc')->get()]);
}
public function submit()
{
$this->validate();
$sendMessage = new Bulletins;
$sendMessage->title = $this->title;
$sendMessage->message = $this->messagesending;
$sendMessage->user_email = $this->user_email;
$sendMessage->expires = $this->expires;
$sendMessage->save();
$this->success_message = 'Message Sent Successfully';
}
}
I really don't get it... I looked for 4 hours now why this is happening.
I ran into this issue as well and found that I was not including the Livewire styles and scripts in my apps layouts blade files.
#livewireStyles
#livewireScripts
I found the difference, not in the logic of the code, but where I was running it from. If I was testing from (localhost/bulletin) I was getting that error. if I included the component inside the dashboard (localhost/home) and ran it from there, then everything worked...
why is that? I can't go to (localhost/bulletin) without being logged in, so I was logged in.

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The POST method is not supported for this route. Supported methods: GET, HEAD

these are my codes for route and controller
routes:
Route::get('/form/create','MyformController#create')->name('form');
Route::post('form', 'MyformController#store');
controller:
public function create()
{
return view('formsubmitted');
//i have put form.create as shown in a laravel tutorial
//but it was showing an error that view form.create is not found, hence i
//changed it to formsubmitted(i created that form)
}
public function store(Request $request)
{
$validateData = $request->validate(
[
'Full Name'=>'required',
'Email'=>'required',
'Feedback'=>'required',
]);
form::create($request->all());
}
I am new to laravel and doing the task of creating a feedback form and storing user info and answer to a database.
I hope to hear from you guys soon. Thank you
/form/create/ and form are two different routes. If you want the same route for the GET and POST function, the routes have to be the same.
Route::get('/form/create','MyformController#create')->name('form');
Route::post('/form/create', 'MyformController#store');
If it is rest api then it might me authentication issue
goto VerifyCsrfToken.php and add there your url for eception
for eg.
protected $except = [
'/anyotherurl',
'/api/userlist'
];

What's the right way to work with MVC and Laravel

I'm building a CRUD using laravel and I'm not sure about the MVC rules.
I thought that all the functions related to database (Crud) should be done inside the model and not the controller. But I found this inside User's Controller:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
I know it's not persisting to the database, just returning a new instance of the class User. Should I call this function inside the model so then persist it ?
Doesn't make much sense calling this to just make a ->save().
Your example is okay, but if you think your controller's doing too much work that it should not be doing, you can refactor your code to transfer the work.
For example, in your code, the password is being bcrypted, you can create a new function in User model (or another helper class if you want, like UserHelper or UserQuery)
class User ...
{
public static function registerUser($data)
{
$data['password'] = bcrypt($data['password']);
$user = self::create($data);
return $user;
}
}
You can now use this to directly pass user data, it will shoulder the bcrypting of the password.
$new_user = User::registerUser(['username' => 'helloworld', 'password' => 'worldhello']);
I think we should always put in mind if a class/method/any other point of control is doing something that is beyond its purpose, then that's the time we should think of refactoring it to another point of control.

Laravel 4.2 form action not working

I'm new to Laravel and am building a simple web application. I'll show what code I'm using next and then I'll explain my problem.
Here's how my form starts in the view login.blade.php:
<?php
//build the form
echo Form::open(array('action' => 'AuthenticationController#authenticateUser'));
And here's what the route for the home page:
Route::get('/', function() {
return View::make('login', array('page_title' => 'Log in to MANGER'));
});
Finally, here's the authentication controller (for now it's a simple redirect to simulate login):
class AuthenticationController extends BaseController {
public function authenticateUser()
{
//retrive from database
return View::make('hr', array('page_title' => 'MANGER Login'));
}
}
My problem is, I'm getting an error on login.blade.php. saying: Route [AuthenticationController#authenticateUser] not defined. (View: /opt/lampp/htdocs/manger/app/views/login.blade.php)
How is the error about a route when I've defined a controller instead? And also, how can this be fixed? Please excuse any noob errors and thanks a lot in advance! :-)
You still need to define your route like this:
Route::post('authenticate', ['uses' => 'AuthenticationController#authenticateUser']);
Otherwise it won't know what method to use, or the url to create.

Codeigniter MVC Sample Code

Hi
I am following the getting started guide for Codeigniterr given at http://www.ibm.com/developerworks/web/library/wa-codeigniter/
I have followed the instruction to create the front view and added controller to handle form submission. Ideally, when i submit the form, it should load the model class and execute the function to put details on the database, but instead it is just printing out the code of the model in the browser.
**Code of view (Welcome.php)**
----------------
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->helper('form');
$data['title'] = "Welcome to our Site";
$data['headline'] = "Welcome!";
$data['include'] = 'home';
$this->load->vars($data);
$this->load->view('template');
}
function contactus(){
$this->load->helper('url');
$this->load->model('mcontacts','',TRUE);
$this->mcontacts->addContact();
redirect('welcome/thankyou','refresh');
}
function thankyou(){
$data['title'] = "Thank You!";
$data['headline'] = "Thanks!";
$data['include'] = 'thanks';
$this->load->vars($data);
$this->load->view('template');
}
}
/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
**Code of Model**
--------------
class mcontacts extends Model{
function mcontacts(){
parent::Model();
}
}
function addContact(){
$now = date("Y-m-d H:i:s");
$data = array(
'name' => $this->input->xss_clean($this->input->post('name')),
'email' => $this->input->xss_clean($this->input->post('email')),
'notes' => $this->input->xss_clean($this->input->post('notes')),
'ipaddress' => $this->input->ip_address(),
'stamp' => $now
);
$this->db->insert('contacts', $data);
}
**OUTPUT after clicking submit**
-----------------------------
class mcontacts extends Model{ function mcontacts(){ parent::Model(); } } function addContact(){ $now = date("Y-m-d H:i:s"); $data = array( 'name' => $this->input->xss_clean($this->input->post('name')), 'email' => $this->input->xss_clean($this->input->post('email')), 'notes' => $this->input->xss_clean($this->input->post('notes')), 'ipaddress' => $this->input->ip_address(), 'stamp' => $now ); $this->db->insert('contacts', $data); }
I have tried doing these things
1. Making all PHP codes executable
2. Change ownership of files to www-data
3. make permission 777 for whole of www
But, the code of model seems to be just printed ... PLEASE HELP
Just a few minor points that might help you:
In your controller, point the index method at the method you would like to call on that page. For example:
function index()
{
$this->welcome();
}
That will help keep things clean and clear, especially if anyone else comes in to work on the code with you later. I chose welcome because that's the name of your controller class and that will keep things simple.
In your model, this:
class mcontacts extends Model{
Should be:
class Mcontacts extends Model{
Capitalize those class names! That could be giving you the trouble you describe.
See here for more info on this: http://codeigniter.com/user_guide/general/models.html
Don't use camel case in your class or method names. This isn't something that will cause your code to fail, but it's generally accepted practice. See Codeigniter's PHP Style guide for more information on this: http://codeigniter.com/user_guide/general/styleguide.html
It's difficult to see with the formatting as it is, but do have an extra curly brace after the constructor method (mcontacts()) in the model? This would cause problems! Also although the code looks generally ok, there are probably better ways to use the framework especially if you do anything more complicated than what you've shown. For example, autoloading, form validation etc. Can I suggest you have a read of the userguide? It's very thorough and clear and should help you alot. http://codeigniter.com/user_guide/index.html

Resources