Do i have to define every controller method in routes.php? - laravel

This is my current routes.php file:
<?php
Route::get('/', 'AdminController#index');
Route::get('/posts','PostsController#index');
Route::get('/posts/create','PostsController#create');
Route::get('/tags','TagsController#index');
Route::get('/health','HealthController#index');
Route::get('/health/create','HealthController#create');
Route::get('/health/categories','HealthController#categories');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController'
]);
If i dont do that for exmaple, i canbt use helpers in blade views , like action. It throws a null exception.
So the question is, do i have to define here all the controller actions? Or else i will not be able to use them directly? For example in a redirect to action link.
Adding this to blade:
<a href="{{ action('PostsController#index') }}">
throws an exception UNLESS i specifically add the route with Route::get NOT working if i add an entry to Route::controllers.
Tried also
<a href="{{ action('\App\Http\Controllers\PostsController#getIndex') }}">
<a href="{{ action('\App\Http\Controllers\PostsController#index') }}">

The problem here are your controller action names. If you use implicit controller routes (Route::controllers) your method names have to start with an HTTP verb.
Instead of index() you need getIndex().
You can easily check what routes Laravel actually registers using the php artisan route:list command.
Generating an URL would then look like this:
<a href="{{ action('PostsController#getIndex') }}">
Note Controller routing only works if you add PostsController to the Route::controllers. (I assumed you did so but just to be sure)
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
'posts' => `PostsController`
]);

Related

Laravel 6 - Named route in blade not allowing special characters or capital letters in parameters

I've recently upgraded to laravel 6 and I've noticed an odd bug. Some of my routes in my blade templates stopped working.
Take this route for example:
Route::post('/create-save-folder/', [
'uses' => 'SaveFolderController#createSaveFolder',
'as' => 'create.save.folder',
'middleware' => ['auth'],
]);
In blade this used to work just fine back in laravel 5.8:
<a href="{{ route('get.save.folder', ['ID' => $folder->ID, 'URL_title' => $folder->URL_title]) }}">
However now it gives me the error:
Missing required parameters for [Route: get.save.folder] [URI:
save-folder/{ID}/{URL_title}]. (View:
C:\xampp\htdocs\MC\resources\views\partials\user_sidebar_block.blade.php)
So I did some debugging. If I change the parameters to random strings, like so:
<a href="{{ route('get.save.folder', ['ID' => 'test', 'URL_title' => 'test']) }}">
that works fine.
So after more debugging I tried changing '$folder->ID' to '$folder->id' in the first parameter.
That ended up working. Which is really weird because if I write something like this in blade:
<p>URL TITLE:{{$folder->URL_title}}</p>
<p>ID:{{$folder->ID}}</p>
it will return the proper results. So that works fine for 'id', but 'URL_title' is still giving me trouble because it has an underscore. So unless I switch my database column to 'urltitle' instead of 'URL_title', I don't know how I'm going to get this route to work.
Why is this happening?
I also had some problems with my blade files when upgrading but not exactly the same as yours.
Doing php artisan view:clear did the trick for me.

Route to grouped routes with prefix correctly in Laravel

I have a group of routes with a prefix.
In my web routes the routes with an admin prefix go to a separate route file:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'as' => 'admin.', 'middleware' => 'admin'], function () {
includeRouteFiles(__DIR__ . '/Admin/');
});
So I have to add admin as prefix in my routes. In my Admin directory I defined the routes as following:
Route::prefix('organization/{organization}')->group(function () {
Route::post('seed', 'SeedController#store')->name('seed');
});
My problem is routing to the routes inside this group. I used the command php artisan route:list to see more info about my routes. It says:
My route name is: admin.seed
My URI is: admin/organization/{organization}/seed
When I link to this route as admin.seed in my form I get the following error:
Missing required parameters for [Route: admin.seed] [URI:
admin/organization/{organization}/seed]. (View:
D:\xampp\htdocs\minute-mn-503\resources\views\admin\organizations\show.blade.php)
I tried linking it as:
admin.seed
admin/seed/1
admin/organization/1/seed
admin/organization/1.seed
But none of them seems to work. This is the line of code for example:
<form method="POST" action="{{ route('admin/organization/'.$organization->id.'.seed') }}">
Any idea on how I might route these correctly? I couldn't find any clear explanation in the Laravel docs.
You must be using this:
<form method="POST" action="{{ url('admin/organization/' . $organization->id .'/seed') }}">
or:
<form method="POST" action="{{ route('admin.seed', $organization->id) }}">

NotFoundHttpException in Handler.php line 103:

I'm using L5.2.
I'm busy trying to create a shopping cart and I've ran into a problem that I can't seem to figure out how it's coming about.
What is suppose to happen is after I've added products to the shopping cart, I click on the shopping cart link and I'm suppose to get taken to another page that says "getCart" echoed on the page.
What is happening is that once I click on the shopping cart link I'm getting the error below and I don't see how I could be getting that error if I'm only echoing out "getCart".
The error I'm getting is
NotFoundHttpException in Handler.php line 103: No query results for model [App\Modules\Menus\Models\Menu].
My routes.php
Route::resource('/', 'OpenController');
Route::get('/{id}', 'OpenController#content');
Route::get('/add-to-cart/{id}', [
'uses' => 'OpenController#getAddToCart',
'as' => 'product.addToCart'
]);
Route::get('/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
My OpenController.php
namespace App\Modules\Open\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\Menus\Models\Menu;
use App\Modules\Portfolio\Models\Portfolio;
use App\Modules\Products\Models\Product;
use App\Modules\Open\Models\Cart;
use Session;
class OpenController extends Controller
{
public function getAddToCart(Request $request, $id){
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->back();
}
public function getCart(){
echo "getCart";
}
}
products.blade.php
<li>
<a href="{{ route('product.shoppingCart') }}">
<i class="fa fa-shopping-cart" aria-hidden="true"></i> Shopping Cart
<span class="badge">{{ Session::has('cart') ? Session::get('cart')->totalQty : '' }}</span>
</a>
</li>
UPDATE:
I've kind of managed to fix it, but I'm hoping someone can still help me out.
I changed my route from
Route::get('/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
to
Route::get('/products/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
Can someone explain why it wouldn't work with just the /shopping-cart
It wouldn't work because "/products/shopping-cart" is a different route that "/shopping-cart".
And since you call route "product.shoppingCart" you also have to have that route declared.
Hope that explained it.

Passing a param containing a path to a controller

I'm trying to pass a variable who contains a path from a form to a controller function in Laravel 4, with the purpose of download an image. I tried a lot of things but nothing worked for me. If I don't pass the parameter in the route, I get a missing parameter error, and if I pass the parameter in the route, I get a NotFoundHttpException.
Here's my code:
View Form:
{{ Form::open(array('route' => array('download', $myPath))) }}
{{ Form::submit('Download', array('class' => 'generator')); }}
{{ Form::close() }}
Route:
Route::post('/download/{myPath}', array('uses' => 'ImagesController#download', 'as' => 'download'));
ImagesController function:
public function download($myPath){
return Response::download($myPath);
}
When I click the submit I'm obtaining a URL like this with NotFoundHttpException:
http://localhost/resizer/public/download/images/myimage.jpg
I don't understand what I'm missing.
You are actually posting two variables. Try this instead
Route::post('/download/{myFolder}/{myFile}', array('uses' => 'ImagesController#download', 'as' => 'download'));
and in your controller
public function download($myFolder, $myFile){
return Response::download($myFolder.'/'.$myFile);
}

Laravel 4: pointing a form to a controller function

I can't understand, how to set up the Form action to direct to a function of a specific controller.
This is my blade code:
{{ Form::open(array('route'=>'user.search')) }}
But I get this error :
Unable to generate a URL for the named route "user.search" as such route does not exist.
the controller (UserController) has a function with this prototype
public function search(){ ... }
I have also tried to set up a route like this in route.php
Route::post('user/search', 'UserController#search');
What is wrong with this code?
You can do it like
{{ Form::open( array('url' => URL::to('user/search')) ) }}
Because you don't have a name for the route. To define a name for the route, use following syntax,
Route::post('user/search', array( 'as' => 'userSearch', 'uses' => 'UserController#search' ));
So, you can use the route by it's name, as
{{ Form::open( array('route' => 'userSearch') ) }} // 'search' method will be invoked
Also, you can directly use the action of a controller as
{{ Form::open( array('action' => 'UserController#search') ) }}
Check Routing and Form.

Resources