why is my web routes going to api routes? laravel 5.4 - laravel

so in my blade files i have these code to do CRUD,
<form method="POST" class="form-horizontal" action="{{ url('tickets/'.$ticket->id) }}"
enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PATCH" />
so i created my api routes. code below. (routes/api.php)
Route::group(['middleware' => ['api']], function () {
Route::resource('/v1/tickets','Api\TicketsController');
});
these are my routes. for web, it's in routes.php, because i got it from a package.
Route::resource($main_route_path, 'App\Http\Controllers\TicketsController', [
'names' => [
'index' => $main_route.'.index',
'store' => $main_route.'.store',
'create' => $main_route.'.create',
'update' => $main_route.'.update',
'show' => $main_route.'.show',
'destroy' => $main_route.'.destroy',
'edit' => $main_route.'.edit',
],
'parameters' => [
$field_name => 'ticket',
],
]);
now, the PROBLEM, is that every time i click the button using the form request, the whole operation goes to the api, any thoughts why is this happening?

All the routes in the api.php are grouped with the prefix, middleware and namespace by default. You don't have to add the api middleware again like you are.
For the web routes you can just use TicketsController since the namespace already points to the Controllers folder.
Can you run php artisan route:list and see what shows up?
Also you can do this to change the name prefix of all resource routes.
Route::resource($main_route_path, 'TicketsController', [
'names' => $main_route,
'parameters' => [
$field_name => 'ticket',
],
]);

Related

Calling same route on submit

I dont know why I am facing this issue.
I have a table where I called new route to open a update view that is
Route::get('update_view/{id}', ['as' => 'update_view', 'uses' => 'admin\study_material\StudyMaterialController#update_view']);
And after submit the form below route is called
Route::post('update/{id}', ['as' => 'update', 'uses' => 'admin\study_material\StudyMaterialController#update']);
Now, issue is when update page is called it is showing update page correctly,but when form is submitted i get error use of post method is not allowed.Use Get or Put.But I check url it is showing me update_view/3 instead of update/3.
Route::group(['prefix' => 'StudyMaterial', 'as' => 'StudyMaterial.'], function () {
Route::get('view', ['as' => 'view', 'uses' => 'admin\study_material\StudyMaterialController#view']);
Route::get('add', ['as' => 'add', 'uses' => 'admin\study_material\StudyMaterialController#add_view']);
Route::post('add_studyMaterial', ['as' => 'add_studyMaterial', 'uses' => 'admin\study_material\StudyMaterialController#add']);
Route::get('update_view/{id}', ['as' => 'update_view', 'uses' => 'admin\study_material\StudyMaterialController#update_view']);
Route::post('update/{id}', ['as' => 'update', 'uses' => 'admin\study_material\StudyMaterialController#update']);
});
My Form :
<form action="{{ route('StudyMaterial.update',$data[0]->id) }}" method="POST" class="text-center" enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" name="_token" id="_token" value="{{ csrf_token() }}">
<button type="submit>Update</button>
</form>
Generated Url :
To View Form to update fileds =>
localhost/project/public/StudyMaterial/update_view/13
To redirect Url to submit form to controller =>
localhost/project/public/StudyMaterial/update/13
after submitting form URL 2 should generate.But here it is showing only Url 1 which is GET method.
This is happening in my whole project.
Thank You in advance
Try this:
<form action="{{ route('StudyMaterial.update',$data[0]->id) }}" method="POST" class="text-center" enctype="multipart/form-data">
#csrf
<button type="submit>Update</button>
</form>

Call a controller Action when using route group

I want to call an action of a controller in laravel blade, when using router group...
route
$router->group([
'namespace' => 'Admin',
'middleware' => 'auth',
], function () {
resource('admin/adsense', 'AdsenseController');
resource('admin/post', 'PostController');
});
So, I want call an action of adsenseController in the blade template
{!! Form::model($var, ['method' => 'PATCH','route' => ['what should i write to call an action ']]) !!}
Example (without router groupe)
route
Route::resource('subject','SubjectController');
blade template
{!! Form::model($var, ['method' => 'PATCH','route' => ['subject.actionName']]) !!}
thanks
If you want to use action, try to use this:
'action' => 'SubjectController#index'
Instead of this:
'route' => ['someroute']
https://laravelcollective.com/docs/5.0/html#opening-a-form
So , i want call an action of adsenseController in the blade template
Why don't you simply point your form's action directly to the controller? Eg
{!! Form::model($var, ['method' => 'PATCH', 'action' => 'Controller#method']) !!}
If you want to redirect to a custom route, it's this simple
{!! Form::model($var, ['method' => 'PATCH', 'route' => 'route.name']) !!}
Check out the documentation on Laravel.com

Laravel 5.2: Session store not set on request. Error for Blade Templates with old()

I am testing with PHPUnit 4.0, Laravel 5.2, PHP 5.5.9, and keep getting the error ERROR: exception 'RuntimeException' with message 'Session store not set on request.' in /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Request.php:85
To rule it out, middleware is enabled.
If I remove {{ old('username') }} from the form, the error goes away. I have seen several posts in regards to this issue using the old() method; however, I have updated the kernel.php, moved the route under a middleware group to reference 'web', even moved start session to the default middleware array() in kernel.php. I have also, tried calling the session from TestCase.php. None of the forums seem to have a working solution. Is there something I'm missing in the syntax, or is there a bug in Laravel?
My kernel.php file looks like
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
protected $routeMiddleware = [
'sso' => \App\Http\Middleware\SsoEnabled::class
];
}
My signup.blade.php looks like:
#extends('layouts.main')
#section('body')
<body>
<h2>Signup</h2>
<form class="m-t" role="form" method="POST" action="/signup">
<div class="form-group">
<input name="username" type="text" placeholder="Username" value="{{ old('username') }}" required="">
</div>
<button type="submit" name="Signup" >Signup</button>
</form>
</body>
#endsection
My routes.php looks like:
<?php
Route::group(['middleware' => ['web']], function () {
Route::group(array('middleware' => ['sso:0']), function ($key) {
Route::get('signup/{token}', [
'as' => 'customer.signup',
'uses' => 'SignupController#getApplication'
]);
});
});
I have even tried adding a new block to the testCase.php setUp without any luck:
$this->app['config']->set('session', [
'driver' => 'array',
'lifetime' => 120,
'expire_on_close' => false,
'encrypt' => false,
'lottery' => [2, 100],
'path' => '/',
'domain' => 'localhost',
'secure' => true,
'email' => 'email#email.com',
'store' => 'storage'
]);
$kernel = app('Illuminate\Contracts\Http\Kernel');
$kernel->pushMiddleware('Illuminate\Session\Middleware\StartSession');
Not sure it's the best solution, but I managed to get it working properly by setting session store in setUp().
\Illuminate\Support\Facades\Request::setSession($this->app['session.store'])

Laravel route not showing as HTTPS in form?

I created a form:
{{ Form::open(array('route' => 'cart.add')) }}
I have this in my routes.php file:
Route::post( 'cart/add', array('https' => true, 'uses' => 'CartController#addToCart', 'as' => 'cart.add'));
Yet, the resulting HTML is:
<form method="POST" action="http://localhost/cart/add" accept-charset="UTF-8">
I was expecting:
<form method="POST" action="https://localhost/cart/add" accept-charset="UTF-8">
because the route is defined as needing HTTPS.
What am I missing?
My problem was that this:
Route::post( 'cart/add', array('https' => true, 'uses' => 'CartController#addToCart', 'as' => 'cart.add'));
Had to be this:
Route::post( 'cart/add', array('https', 'uses' => 'CartController#addToCart', 'as' => 'cart.add'));

missing argument on laravel update

I am working with laravel 4. I have created an edit form like below:
{{ Form::model($v, array('route' => array('insur_docs.update', $v->id),'method' => 'PUT','class'=>'form-horizontal')) }}
and update route
Route::put('insur_docs/update', array('as' => 'insur_docs.update', 'uses' => 'Insur_DocController#update'));
The problem it shows is:
Missing argument 1 for Insur_DocController::update()
The problem is in your route, you need to add /{id} to it. Here's a test I did:
class Insur extends Eloquent {
}
Route::put('insur_docs/update/{id}', array('as' => 'insur_docs.update', 'uses' => 'Insur_DocController#update'));
Route::get('test', function() {
$v = new Insur;
$v->id = 1;
return Form::model($v, array('route' => array('insur_docs.update', $v->id),'method' => 'PUT','class'=>'form-horizontal'));
});
And the generated form open was:
<form method="POST" action="http://localhost/insur_docs/update/1" accept-charset="UTF-8" class="form-horizontal"><input name="_method" type="hidden" value="PUT">
<input name="_token" type="hidden" value="V0TP6LbCjO1kGF6LCLObEi6hofbW5ZgNo5Kz7nQ3">
Route::put('insur_docs/update/{id}', array('as' => 'insur_docs.update', 'uses' => 'Insur_DocController#update'));

Resources