Laravel 5 GET inside controller? - php

I use Laravel5 and i cant understand why postProcess or getProcess not works?
Example::
html page:
{{ Form::open(array('url' => 'portfolio/process')) }}
{{ Form::submit() }}
{{ Form::close() }}
route:
Route::resource('portfolio','PortfolioController');
controller:
public function postProcess (){
return 'Text!';
}
Every time i get error:
MethodNotAllowedHttpException in RouteCollection.php line 218:

It doesn't work because Route::resource doesn't build these routes and you need to explicitly define them:
Route::post('portfolio/process', 'PortfolioController#postProcess');

I think you need to check your route list:
Run this command php artisan route:list in terminal and check your route.
Hope this work for you!

Related

How to get a laravel route group to work

I am trying to set up a Laravel route group in Laravel 5.5 and use it in a blade. However I am getting the a Route not defined error. The full error is :
"Route [admin/route_group_test] not defined. (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php) (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php) (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php)
I have looked through the documentation and it looks like I am doing it right. Here is the line from the route file:
Route::prefix('admin')->group(function(){
Route::get('route_group_test','AdminController#testingMiddleWare');
});
and the link from the blade:
{{route('admin/route_group_test')}}
I have no idea what I am doing wrong
The route() helper uses route's name. From the docs:
The route function generates a URL for the given named route
So you need to name the route:
Route::get('route_group_test', 'AdminController#testingMiddleWare')->name('admin.route_group_test');
Or:
Route::get('route_group_test', ['as' => 'admin.route_group_test', 'uses' => 'AdminController#testingMiddleWare']);
And then use it:
{{ route('admin.route_group_test') }}
Or you can use unnamed route:
{{ url('admin/route_group_test') }}
Try the below code:
Route::group(['prefix'=>'admin','namespace'=>''], function () {
Route::get('route_group_test','AdminController#testingMiddleWare');
});

Go back URL in Laravel 5.1

How can I get the previous URL visited on the website in Laravel 5.1?
In Laravel 4 I just needed to write it like below:
{{ URL::previous() }}
The cleanest way seems to be using the url() helper:
{{ url()->previous() }}
URL::previous() works for me in my Laravel 5.1 project. Here is Laravel 5.1 doc for previous() method, which is accessible through URL Facade.
You can still try alternatives, in your views you can do:
{{ redirect()->getUrlGenerator()->previous() }}
or:
{{ redirect()->back()->getTargetUrl() }}

Routing in laravel 5 with resource

I've a route:
Route::resource('partner/register', 'PartnerController\Register');
And function index, works. But when I try route to store with:
{!! Form::open(['route'=>'partner/register.store']) !!}
Error:
Route [partner/register.store] not defined.
Why? help please.
PD: I used artisan make:controller PartnerController/Register to create the controller. Because need of create controllers in differents directories
This part is fine I guess as you said you needed it in different directories
Route::resource('partner/register', 'PartnerController\Register');
But to get the store route use
{!! Form::open(['url' => route('partner.register.store')]) !!}
Btw you can see all the current routes with php artisan route:list

Laravel 5: Class 'HTML' not found

I'm just getting started with Laravel. I'm in a controller method and I say:
return \View::make('scrape', $data);
Then in scrape.blade.php I have:
#extends('layouts.master');
Finally, in layouts/master.blade.php I have:
{{ HTML::style('css/bootstrap.min.css') }}
And that where things seem to fall apart and I get:
FatalErrorException in 002eb18bb71fd3ec1de058967b799d49 line 6:
Class 'HTML' not found
What am I doing wrong? Thanks for your help.
Searching on google I found this
"By default in Laravel 5.0, Html and Form are not embedded anymore."
You need to add this package to you application.
Please use above links and last change HTML to Html.
eg:
{{ HTML::style('css/bootstrap.min.css') }}
to
{{ Html::style('css/bootstrap.min.css') }}.
its working.

MethodNotAllowedHttpException on resource defined method Laravel-4

I created a very simple form so that I could use a submit button rather than a link to open up an edit users page. Using a link works perfectly, but the form button fails and yields a MethodNotAllowedHttpException even though the method ("edit") is perfectly defined in the UsersController resource and otherwise works fine.
Route:
Route::resource('users','UsersController');
UsersController:
public function edit($id)
{
$user = $this->user->find($id);
return View::make('users.edit')->with('user',$user);
}
show.blade.php:
<!-- This works fine: -->
{{ link_to_route('users.edit', ("Edit: " .$user->first_name." ".$user->last_name), $user->id) }}
<!-- This doesn't work, and yields the Method Not Allowed exception: -->
{{ Form::open(array('route' => array('users.edit',$user->id))) }}
{{ Form::submit('Edit User', array('class'=>'button')) }}
{{ Form::close() }}
Thanks.
When you do Form::open(), it defaults to using the post request method. But when you create a Route::resource(), the edit method takes a get request.
To make it work through the form, you'll need to open it with an additional parameter, like this:
{{ Form::open(array('route' => array('users.edit',$user->id),
'method' => 'get')) }}
You need to point to the update route, not edit.
{{ Form::open(array('route' => array('users.update', $user->id))) }}
The edit route is for displaying the view, while the update is for the put/patch request.
For more information about using the RESTful routes, I'd recommend checking out http://laravel.com/docs/controllers#resource-controllers

Categories