The POST method is not supported for this route. - Laravel - php

I have a problem with my create post page. When I submit I get this error:
The POST method is not supported for this route. Supported methods: GET, HEAD.
I have no clue where it comes from as I am pretty new to Laravel.
routes(web.php):
<?php
Route::get('/', [PagesController::class, 'index']);
Auth::routes();
Route::get('/blog', 'App\Http\Controllers\PostsController#index');
Route::get('/blog/create', 'App\Http\Controllers\PostsController#create');
Route::get('/logout', 'App\Http\Controllers\Auth\LoginController#logout', function (){
return abort(404);
});
Route::get('/home', 'App\Http\Controllers\HomeController#index');
Controller:
public function index()
{
return view('blog.index')
->with('posts', Post::orderBy('updated_at', 'DESC')->get());
}
public function create()
{
return view('blog.create');
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|mimes:jpg,png,jpeg|max:5048'
]);
$newImageName = uniqid() . '-' . $request->title . '.' .
$request->image->extension();
$request->image->move(public_path('images'), $newImageName);
Post::create([
'title' => $request->input('title'),
'description' => $request->input('description'),
'slug' => SlugService::createSlug(Post::class, 'slug',
$request->title),
'image_path' => $newImageName,
'user_id' => auth()->user()->id
]);
return redirect('/blog')
->with('message', 'Je post is toegevoegd!');
}
image for reference

You are using get for all your routes... You have to use Route::post... Read the documentation again about this topic so you can understand it better.
Remember that using Route::post, Route::get, Route::delete and more, is the method you are allowing the route to be accessed with.

It comes from your Routes:
Route::get('/', [PagesController::class, 'index']);
Auth::routes();
Route::get('/blog', 'App\Http\Controllers\PostsController#index');
Route::get('/blog/create', 'App\Http\Controllers\PostsController#create');
Route::get('/logout', 'App\Http\Controllers\Auth\LoginController#logout', function (){
return abort(404);
});
Route::get('/home', 'App\Http\Controllers\HomeController#index');
You have GET routes, but no POST routes. You need Route::post in order to support POST.

You syntax for registering the route for post method is incorrect.
It should be: Route::post($uri, $callback); where callback can be any function in controller layer or service layer.
If you want to register your route for multiple http verbs, you can do so using any of the below methods.
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
Refer to this documentation. It explains the concept in simple terms.

Related

Laravel $id return 'en' instead of id

My route:
Route::group([
'prefix' => '{locale}',
'where' => ['locale' => '[a-zA-Z]{2}'],
'middleware' => 'setlocale'],
function () {
// GROUP FOR AUTHENTICATION
Route::group([
'middleware' => 'auth:sanctum', 'verified',
], function () {
// GROUP FOR ADMIN
Route::group([
'prefix' => 'admin',
'as' => 'admin.',
], function () {
Route::resource('partner', PartnerFormController::class);
});
});
});
My view that when I click, it shows the details page:
Edit
My controller:
public function show(PartnerForm $partnerForm, $id)
{
$details = DB::table('partner_forms')->where('id', $id)
->first();
return view('admin.partner-details', compact('details'));
}
I tried to call $details->name or $details->in my view details page but it didnt work. The URL is working properly by displaying http://127.0.0.1:8000/en/admin/partner/1 but when i dd() my $id in controller it returns en, which I believe is the en from the URL.
Instead of sending the ID along with the route, send all the variables and enter the information into the new page in the control without the need to connect to the database and using route model binding.
Example :
view A
Foo
route
Route::get('.../{partner}', [Controller::class, 'Bar'])->name('admin.partner.show');
controller
public function Bar(Partner_MODEL $partner){
return view('view_name', compact('partner'));
}
view B : use $partner.
Important note : in (route, controller method, compact meethod) The variable name must be similar

Controller is returning a blank data

the controller is returning a blank data/view and I think something is wrong with my routes. if I remove {locale}, the data is retrieved.
Can anyone help with returning the data properly while my routes have {locale} in it? Here are my related code:
Web.php
Route::get('{locale}/projects/{id}/billings', 'ProjectController#showbilling')
->name('showbilling');
Route::post('{locale}/projects/{id}', 'ProjectController#addbilling')
->name('addbilling');
ProjectController.php
public function showbilling($id)
{
$billings = Project::find($id);
$locale = app()->getLocale();
return $billings;
//return view('admin.addbillings', compact('billings'));
}
Edit: Here's my full web.php
web.php
Route::get('/', function() {
return redirect(app()->getLocale());
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function () {
Route::get('/', function () {
return view('welcome');
})->name('main');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
//Customers
Route::get('/customers', 'CustomerController#showcust')->name('customers');
Route::post('/sendcust', 'CustomerController#sendcust')->name('sendcust');
//Items
Route::get('/items', 'ItemController#showitems')->name('items');
Route::post('/senditem', 'ItemController#senditem')->name('senditem');
//Projects
Route::get('/projects', 'ProjectController#showprojects')->name('projects');
Route::post('/sendproj', 'ProjectController#sendproj')->name('sendproj');
//ProjectBillings
Route::get('/projects/{id}/billings', 'ProjectController#showbilling')->name('showbilling');
Route::post('/projects/{id}', 'ProjectController#addbilling')->name('addbilling');
//Invoices
Route::get('/invoices', 'InvoiceController#showinvoice')->name('invoices');
Route::post('/sendinvoitem', 'InvoiceController#sendinvoitem')->name('sendinvoitem');
Route::get('/invoices/{id}/details', 'InvoiceController#showdetails');
Route::post('/updateitem','InvoiceController#updatedetail')->name('updateitem');
Route::get('invoices/{id}/generate', 'InvoiceController#generate');
Route::post('/updatestatus', 'InvoiceController#changestatus')->name('updatestatus');
});
You are passing 2 params in your route but accepting only 1 in the controller. Add locale.
public function showbilling($locale, $id)

How to pass extra parameters to controller from Laravel route

I'm trying to handle basic validation of my API calls in the Laravel's routes. Here is what I want to achieve:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', 'PropertiesController#getPropertyByProgressStatus', function () {
//pass variable x = 1 to the controller
});
Route::get('waiting', 'PropertiesController#getPropertyByProgressStatus', function () {
//pass variable x = 2 to the controller
});
});
Long story short, depending on the segment of the URI after api/v1/properties/ I want to pass a different parameter to the controller. Is there a way to do that?
I was able to get it to work with the following route.php file:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 1
]);
Route::get('remodeled', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 1
]);
Route::get('pending', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 3
]);
Route::get('available', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 4
]);
Route::get('unavailable', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 5
]);
});
and the following code in the controller:
public function getPropertyByProgressStatus(\Illuminate\Http\Request $request) {
$action = $request->route()->getAction();
print_r($action);
Pretty much the $action variable is going to let me access the extra parameter that I passed from the route.
I think that you can do it directly in the controller and receiving the value as a parameter of your route:
First you need to specify the name of the parameter in the controller.
Route::group(['prefix' => 'api/v1/properties/'], function ()
{
Route::get('{parameter}', PropertiesController#getPropertyByProgressStatus');
In this way, the getPropertyByProgressStatus method is going to receive this value, so in the controller:
class PropertiesController{
....
public function getPropertyByProgressStatus($parameter)
{
if($parameter === 'purchased')
{
//do what you need
}
elseif($parameter === 'waiting')
{
//Do another stuff
}
....
}
I hope it helps to solve your issue.
Take a view for this courses: Learn Laravel or Create a RESTful API with Laravel
Best wishes.
----------- Edited ---------------
You can redirect to the route that you want:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', function () {
return redirect('/api/v1/properties/purchased/valueToSend');
});
Route::get('waiting', function () {
return redirect('/api/v1/properties/waiting/valueToSend');
});
Route::get('purchased/{valueToSend}', PropertiesController#getPropertyByProgressStatus);
});
Route::get('waiting/{valueToSend}', PropertiesController#getPropertyByProgressStatus);
});
});
The last two routes response to the redirections and send that value to the controller as a parameter, is the most near that I think to do this directly from the routes.

Laravel Routing resource controller

I am using laravel rource controller and routes and having problem in view file while using Form::Open method
Following is my code in routes.php
$router->group(['namespace' => 'Admin', 'middleware' => 'auth'],
function (){ resource('admin/post', 'PostController',
['only' => ['index', 'create', 'store', 'newe', 'afadfafafa']]);
resource('admin/tag', 'TagController');
get('admin/upload', 'UploadController#index');
});
In view.php
{!! Form::open(array('action' => 'Admin\PostController#store')) !!}
In Controllers/Admin/AdminController.php I do have a method named store.
Still I am getting, form action url renders as "http://localhost/laravel/admin/post" i.e. to index action and not store action.
What is problem in my code.
i try your code and it works right
Admin\PostController.php
public function index()
{
echo "method index";
}
public function store(Request $request)
{
echo "method store";
}
This is what i am expecting with current code should work

Laravel redirect to user dashboard when visiting login page if logged in

I have a basic login system setup, but I would like the user to be sent to there dashboard page if they try to access the "login" page or "create account" page.
How do I go about doing this?
I am thinking something in the routes file?:
Route::post('/login', array('uses' => 'UserController#login'));
Route::post('/create-account', array('uses' => 'UserController#createAccount'));
Route::group(array('before' => 'auth'), function () {
Route::get('/dashboard', array('uses' => 'DashboardController#index'));
Route::get('/logout', function () {
Auth::logout();
return Redirect::to('/start');
});
});
Perhaps some kind of group around the first two routes?
A before filter is perfect for this. Since it basically will do the opposite of auth let's call it no_auth:
Route::filter('no_auth', function(){
if(Auth::check()){
return Redirect::to('dashboard');
}
}
And then wrap a group around your two routes to apply the filter:
Route::group(array('before' => 'no_auth'), function(){
Route::post('/login', array('uses' => 'UserController#login'));
Route::post('/create-account', array('uses' => 'UserController#createAccount'));
});
Edit
Actually, as #afarazit points out, there's already a filter like that in app/filters.php called guest. You just have to change the redirect URL to dashboard and you're ready to go.
There's already a filter for what you want, check your filters.php for "guest"
There are many ways to do this. You can use an Event listener like so:
Event::listen('user.login', function (){
if(Auth::check()){
return Redirect::to('dashboard');
}
});
Event::listen('user.create', function (){
if(Auth::check()){
return Redirect::to('dashboard');
}
});
You need a named controller for above like so:
Route::post('/login', array(
'uses' => 'UserController#login',
'as' => 'user.login'
));
Route::post('/create-account', array(
'uses' => 'UserController#createAccount',
'as' => 'user.create'
));
You may use a constructor and include a filter in it. Here is an example; You can modify your code according to the example.
public function __construct(SignInForm $signInForm)
{
$this->signInForm = $signInForm;
$this->beforeFilter('guest', ['except' => 'destroy']);
}
If laravel version is 4.2
open your app/filters.php and add
Route::filter('no_auth', function(){
if(Auth::check()){
return Redirect::to('dashboard');
}
});
add your login and create account pages to your app/routes.php like below:
Route::group(array('before' => 'no_auth'), function(){
Route::post('/login', array('uses' => 'UserController#login'));
Route::post('/create-account', array('uses' => 'UserController#createAccount'));
});
It worked for me.

Categories