Laravel Livewire The POST method is not supported for this route - laravel

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.

Related

Can't get Livewire Events Emit and Listen to work

I am trying to setup an event listener, so that when a child livewire component gets the title updated, it would refresh the parent component to show the update instead of having to hard refresh the page to see the update.
This is a quick gif showing what is taking place
https://i.gyazo.com/faefb27c2fe0fb32da097fbbf5cc1acb.mp4
I have 2 livewire components.
Parent = ViewSidebar.php / view-sidebar.blade.php
// view-sidebar.blade.php
#foreach ($kanbans as $kanban )
#livewire('kanbans.show-sidebar-kanban', ['kanban'=>$kanban], key($kanban->id))
#endforeach
// ViewSidebar.php
public $kanbans;
protected $listeners = ['refreshKanbans'];
public function refreshKanbans()
{
$this->kanbans = Kanban::where('status', $this->active)
->orderBy('order', 'ASC')
->get();
}
public function mount()
{
$this->refreshKanbans();
}
public function render()
{
return view('livewire.kanbans.view-sidebar');
}
In the child component I set this
public function updateKanban($id)
{
$this->validate([
'title' => 'required',
]);
$id = Kanban::find($id);
if ($id) {
$id->update([
'title' => $this->title,
]);
}
$this->resetInputFields();
$this->emit('refreshKanbans');
}
All of the files are in a subfolder called kanbans could this be breaking it?
Trying to follow along these docs https://laravel-livewire.com/docs/2.x/events
I also tried this approach with calling the emit $this->emit('updateKanbanSidebar'); and setting the listener like this protected $listeners = ['updateKanbanSidebar' => 'refreshKanbans'];
Clearly I am understanding the documentation wrong, but no clue where the issue is.
Any help is much appreciated!
Thank you in advance :)
There is something wrong in your code, so let me help you with. After emit from child (be sure this is doing well) just need have this in parent component
Parent
protected $listeners = ['refreshKanbans' => '$refresh'];
public function render()
{
$this->kanbans = Kanban::where('status', $this->active)
->orderBy('order', 'ASC')
->get();
return view('livewire.kanbans.view-sidebar');
}
Let me know about
I was able to get this to work using this in the child component, and skipping the emits. I was able to DD and the emit was working properly, but not sure why it wasn't updating.
public function updateKanban($id)
{
$this->validate([
'title' => 'required',
]);
$this->kanban->update(['title' => $this->title]);
$this->resetInputFields();
$this->kanban=Kanban::find($this->kanban->id);
}

Laravel 5: How to create a router model binding on multiple parameters

So far I know how to create a router model binding on single parameters like so:
// RouteServiceProvider.php
$router->model('subject_slug', 'App\Subject', function($slug) {
return Subject::where('slug', $slug)->firstOrFail();
});
The above can then be used like this:
// routes.php
Route::get('/{subject_slug}', 'MenuController#showSubject');
And in the controller:
public function showSubject(Subject $subject) {
....
}
But sometimes I need to specify multiple parameters in order to get the right model.
For example consider the following route:
Route::get('/{subject_slug}/{topic_slug}/', 'MenuController#showTopic');
and the corresponding controller:
public function showTopic(Subject $subject, Topic $topic) {
....
}
However to get the correct model for Topic I need to know the Subject. For example:
// !!! Invalid laravel code !!!
$router->model('topic_slug', 'App\Topic, function($subject_slug, $topic_slug) {
// ERROR: $subject_slug is obviously not defined!
return Topic::where([
'subject_slug' => $subject_slug,
'slug' => $topic_slug,
])->firstOrFail();
});
How can I make a router model binding for Topic bearing in mind I need to know the Subject parameter before it in order to fetch the correct Topic.
Is there an alternative better way of doing this?
UPDATE
Currently my showTopic method in my controller is like this:
public function showTopic(Subject $subject, $topic_slug) {
$topic = Topic::where([
'subject_slug' => $subject_slug,
'slug' => $topic_slug,
])->firstOrFail();
// ...
}
and I have no router model binding for topic_slug.
This works as expected, but I would like to take advantage of router model bindings!
It turns out the way I was doing it was a bit flawed. I was unnessarily using model bindings when instead it would be better to have used a normal binding like so:
$router->bind('topic_slug', function($slug, Route $route) {
$subject = $route->parameter('subject_slug');
return Topic::where([
'subject_slug' => $subject->slug,
'slug' => $slug,
])->firstOrFail();
});
Also I was using model bindings completely wrong before as the 3rd function should be the "not found behaviour" (not for additional logic)!

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.

Session is not read in multiple controllers in cakephp

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

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