Connect Route::resource with existing model - php

I am using Laravel 5.4 and I have created a controller with resources. Now I would like to open the edit page in my app like the following:
a href="{{ route('tasks.edit', ['tasks'=>$storedTask->id]) }}" class='btn btn-default'>Edit</a>
In my view I am using:
Route::resource('/', 'IndexController');
However, I get back
Sorry, the page you are looking for could not be found when opening the link.
When looking into my routes list the URIs are emtpy.
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | / | index | App\Http\Controllers\IndexController#index | web |
| | POST | / | store | App\Http\Controllers\IndexController#store | web |
| | PUT|PATCH | {} | update | App\Http\Controllers\IndexController#update | web |
| | DELETE | {} | destroy | App\Http\Controllers\IndexController#destroy | web |
| | GET|HEAD | {} | show | App\Http\Controllers\IndexController#show | web |
| | GET|HEAD | {}/edit | edit | App\Http\Controllers\IndexController#edit | web |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
I guess that my model is not "associated" with my controller and therefore the Route::resource view.
Any suggestions how to fix this?

You can't use resource route with just /. Change it to:
Route::resource('tasks', 'IndexController');

Related

Make a difference between Show and create routes

I have all those routes:
Route::resource('tournaments', 'TournamentController');
And in TournamentController, I have :
public function __construct()
{
$this->middleware('auth')->except('show');
}
But When I go to http://laravel.dev/tournaments/create, Laravel thinks create is the tournament slug ( like http://laravel.dev/tournaments/my-tournament/ ) and send me to #show instead of #create.
In this case, I get a Model Not Found coming from SubstituteBindings Middleware...
I don't really understand this one, how can I avoid this confusion???
EDIT: Also list of my routes :
POST | tournaments | tournaments.store | App\Http\Controllers\TournamentController#store | web,ownTournament,auth |
GET|HEAD | tournaments | tournaments.index | App\Http\Controllers\TournamentController#index | web,auth |
GET|HEAD | tournaments/create | tournaments.create | App\Http\Controllers\TournamentController#create | web,ownTournament,auth |
GET|HEAD | tournaments/deleted | | App\Http\Controllers\TournamentController#getDeleted | web,ownTournament,auth |
GET|HEAD | tournaments/{tournament} | tournaments.show | App\Http\Controllers\TournamentController#show | web |
PUT|PATCH | tournaments/{tournament} | tournaments.update | App\Http\Controllers\TournamentController#update | web,ownTournament,auth |
DELETE | tournaments/{tournament} | tournaments.destroy | App\Http\Controllers\TournamentController#destroy | web,ownTournament,auth |
GET|HEAD | tournaments/{tournament}/edit | tournaments.edit | App\Http\Controllers\TournamentController#edit | web,ownTournament,auth |
EDIT: Now I have the same problem with another route:
Route::get('/tournaments/deleted', 'TournamentController#getDeleted');
I get a Model not found by SubstituteBindings Middleware.
I guess Middle is trying to resolve deleted as a tournament name...
Case re-opened!
I found the issue. I had this route:
Route::get('tournaments/{tournament}', 'TournamentController#show')->name('tournaments.show');
at the beginning of my route file.
My bad! I let the answer here for people who get the same problem!

Route::resource change path names in Laravel 5.2?

When using RESTful resource controllers, we can use route::resource() to work with, but the URIs seem to be default.
+--------+-----------+--------------------+---------------+---------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------+---------------+---------------------------------------------+------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | items | items.index | App\Http\Controllers\ItemController#index | web |
| | POST | items | items.store | App\Http\Controllers\ItemController#store | web |
| | GET|HEAD | items/create | items.create | App\Http\Controllers\ItemController#create | web |
| | GET|HEAD | items/{items} | items.show | App\Http\Controllers\ItemController#show | web |
| | PUT|PATCH | items/{items} | items.update | App\Http\Controllers\ItemController#update | web |
| | DELETE | items/{items} | items.destroy | App\Http\Controllers\ItemController#destroy | web |
| | GET|HEAD | items/{items}/edit | items.edit | App\Http\Controllers\ItemController#edit | web |
| | POST | register | signup | App\Http\Controllers\UserController#SignUp | web |
| | GET|HEAD | signup | | Closure | web |
+--------+-----------+--------------------+---------------+---------------------------------------------+------------+
(ignore the first and the last two, we're looking at the 'items.something' routes)
I want to, for example, change "items/create" to "items/new".
On a previous question, the answer was "No", but since the question is over one year old and Laravel developments seems to be pretty fast, is there already a solution?
The answer hasn't really changed. You can't customize a resource controller action directly. There is a workaround however: you can exclude it and add it yourself.
First make your resource route partial and exclude the action(s) you don't want:
https://laravel.com/docs/5.2/controllers#restful-partial-resource-routes
Route::resource('items', 'ItemController', ['except' => [
'create'
]]);
Then you can add your own routes in addition:
https://laravel.com/docs/5.2/controllers#restful-supplementing-resource-controllers
Route::get('items/new', 'ItemController#new');
Make sure to stick that before the resource route, as mentioned in the docs.
Note you can customize the route name, as mentioned in that previous thread you linked to.

Laravel missing routes issue

i have a problem with my Laravel routes.
if i call the following url: http://laravel/market it works fine, but my site has different languages so i use the LaravelLocalization package and when i call http://laravel/en/market it comes an error:
Route [market.offers.show] not defined.
i have used:
php artisan route:list
to see the indexed routes and here is a shortcut of it:
| GET|HEAD | market/offers | market.offers.index | App\Http\Controllers\Front\OffersController#index
| POST | market/offers | market.offers.store | App\Http\Controllers\Front\OffersController#store
| GET|HEAD | market/offers/bid/{id} | market.offers.bid | App\Http\Controllers\Front\OffersController#bid
| GET|HEAD | market/offers/create | market.offers.create | App\Http\Controllers\Front\OffersController#create
| GET|HEAD | market/offers/history | market.offers.history | App\Http\Controllers\Front\OffersController#history
| POST | market/offers/store/bid | market.offers.store.bid | App\Http\Controllers\Front\OffersController#storebid
| DELETE | market/offers/{offers} | market.offers.destroy | App\Http\Controllers\Front\OffersController#destroy
| GET|HEAD | market/offers/{offers} | market.offers.show | App\Http\Controllers\Front\OffersController#show
| PUT|PATCH | market/offers/{offers} | market.offers.update | App\Http\Controllers\Front\OffersController#update
| GET|HEAD | market/offers/{offers}/edit | market.offers.edit | App\Http\Controllers\Front\OffersController#edit
on other pages i get the same error for Route [market.offers.create]
why is that ? How can i resolve that ?
in your routes you should add a wildcard for the language for example it will look like /laravel/{lang}/market
and then in your controllers you handle the if the lang is null or not to set the default language

Laravel5 make:auth doesn't work

I used the command make:auth and it showed:
Created View: /var/www/myblog/resources/views/auth/login.blade.php
Created View: /var/www/myblog/resources/views/auth/register.blade.php
Created View: /var/www/myblog/resources/views/auth/passwords/email.blade.php
Created View: /var/www/myblog/resources/views/auth/passwords/reset.blade.php
Created View: /var/www/myblog/resources/views/auth/emails/password.blade.php
Created View: /var/www/myblog/resources/views/layouts/app.blade.php
Created View: /var/www/myblog/resources/views/home.blade.php
Created View: /var/www/myblog/resources/views/welcome.blade.php
Installed HomeController.
Updated Routes File.
Authentication scaffolding generated successfully!
It looks like great and I got a homepage showing up correctly.
see picture here
But when I was trying to access Login and Register page, something went wrong.
It gave me errors like:
Not Found
The requested URL /login was not found on this server.
Apache/2.4.7 (Ubuntu) Server at ...
and I typed php artisan route:list in terminal I got this:
+--------+-----------+-------------------------+-----------------+-----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-------------------------+-----------------+-----------------------------------------------------------------+------------+
| | GET|HEAD | / | | Closure | |
| | GET|HEAD | article | article.index | App\Http\Controllers\ArticleController#index | |
| | POST | article | article.store | App\Http\Controllers\ArticleController#store | |
| | GET|HEAD | article/create | article.create | App\Http\Controllers\ArticleController#create | |
| | DELETE | article/{article} | article.destroy | App\Http\Controllers\ArticleController#destroy | |
| | PUT|PATCH | article/{article} | article.update | App\Http\Controllers\ArticleController#update | |
| | GET|HEAD | article/{article} | article.show | App\Http\Controllers\ArticleController#show | |
| | GET|HEAD | article/{article}/edit | article.edit | App\Http\Controllers\ArticleController#edit | |
| | GET|HEAD | home | | App\Http\Controllers\HomeController#index | web,auth |
| | GET|HEAD | login | | App\Http\Controllers\Auth\AuthController#showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\AuthController#login | web,guest |
| | GET|HEAD | logout | | App\Http\Controllers\Auth\AuthController#logout | web |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController#sendResetLinkEmail | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController#reset | web,guest |
| | GET|HEAD | password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController#showResetForm | web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\AuthController#showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\AuthController#register | web,guest |
+--------+-----------+-------------------------+-----------------+-----------------------------------------------------------------+------------+
It's a bit different from what I expect. Cuz from what I learned in laracast.com, the route list should be like "Auth\login" instead of merely "login"... Idk what's going wrong here, is it because I'm using apache? or other configurations I didn't set up correctly... Looking for help
Also another question:
when I access www.mydomain.com, I'd expect the default welcome page instead of auth's home page but actually it gives me the auth's home page instead. But when I was trying to access www.mydomain.com/home which I expect to see that home page again, instead I received 404 not found errors which confused me a lot. here is my routes.php:
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::resource('article', 'ArticleController');
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
});
Hi Guys I just solved this problem.
This problem is definitely unrelated to laravel and is totally caused by Apache2 settings.
What I did is simply
sudo a2enmod rewrite
and restart service.

Change default RESTFul actions laravel

I created a controller named CatController with php artisan make:controller CatController. So it generated the next route list:*-> What generated the route list is on routes.php Route::resource('cat','CatsController');
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------+-------------+-------------------------------------------------+------------+
| | GET|HEAD | / | | Furbook\Http\Controllers\CatController#index | |
| | GET|HEAD | cat | cat.index | Furbook\Http\Controllers\CatController#index | |
| | POST | cat | cat.store | Furbook\Http\Controllers\CatController#store | |
| | GET|HEAD | cat/create | cat.create | Furbook\Http\Controllers\CatController#create | |
| | DELETE | cat/{cat} | cat.destroy | Furbook\Http\Controllers\CatController#destroy | |
| | PATCH | cat/{cat} | | Furbook\Http\Controllers\CatController#update | |
| | PUT | cat/{cat} | cat.update | Furbook\Http\Controllers\CatController#update | |
| | GET|HEAD | cat/{cat} | cat.show | Furbook\Http\Controllers\CatController#show | |
| | GET|HEAD | cat/{cat}/edit | cat.edit | Furbook\Http\Controllers\CatController#edit | |
Later on I thought it would be better to call it CatsController and handle the urls as cats/... so I renamed the controller but I still have the same default REST actions URIs.
Is there anyway to change it? How should I proceed?
To my knowledge the make:controller command only generates the controller file, not any route definitions. The routes defined there look like they've beed generated by a Route::resource definition that would look like this:
Route::resource('cat', 'CatController');
To make that work with cats and CatsControllers you should change it to this:
Route::resource('cats', 'CatsController');
You can read more on RESTful Resource Controllers in the Laravel Documentation.

Categories