Laravel named routes not working - php

Recently I switched to Laravel 5.3.
I have the following route
Route::get('/activate/token', 'AccountActivationController#activate')->name('auth.activate');
But, when I use
dd(route('auth.activate'));
I get the following error:
InvalidArgumentException in UrlGenerator.php line 314: Route [auth.activate] not defined.
It works perfectly fine with
Route::get('/activate/token', [
'as' => 'auth.activate',
'uses' => 'AccountActivationController#activate',
]);
Is this new in Laravel 5.3?? I am fairly new to Laravel itself.
Thank You.

For anyone that comes across this in the future, it is because you aren't refreshing the name lookup on the router.
Laravel 5.2 added the fluent method name($name) as a shortcut replacement for ['as' => $name], but the name($name) method requires $router->getRoutes()->refreshNameLookups(); to be called at some point after your routes are registered in order to actually complete that mapping internally.
In it's current form, the RoutingServiceProvider implemented in the example laravel/laravel package handles this for you behind the scenes, but if you're loading routes in any sort of custom way, you'll need to trigger that refresh yourself at an appropriate time.
See https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php#L47-L50 for how it's handled in Laravel 8.x

Missing a tick mark
Try
dd(route('auth.activate'));
instead.

Related

How to remove default index action on controller from laravel 5.5

I am using Laravel 5.5 version. I defined my routes in the routes.php file. like this:-
$router->group(['middleware' => 'auth'], function($router) {
$router->resource('/route-name', 'myController#myMethodName');
});
But when I run my application laravel gives error:-
Method [myMethodName#index] does not exist on [App\Http\Controllers\myController].
It is by default put index action after my defined action in the routes.
It is working fine in the laravel 5.3 version. Please solve my problem..
Try this as #devk told in comment:
$router->get('/route-name', 'myController#myMethodName');
By Default resource Request create CRUD requests in routes. For the following methods in Controller.
Index(GET)
Create(GET)
Store(POST)
Show(GET)
edit(GET)
Update(PUT/PATCH)
Delete(DELETE)
You can't pass method name in resource route.
If you want to override any of them. you have to write new one route below the resource Route. Like
Route::get('url','Controller#newMethod');
And Change the method name in Controller with newMethod
For more detail check laravel documents

Laravel Routing Parameter Contraint

Been working on a neat little project. I'm using Laravel 5.5, and I'm building routes to handle the various requests. I've got a route that accepts a slug to locate a particular guild via route model binding. Works great! Beautifully, actually. Then, I defined a "static" route that doesn't use a parameter to show the form for creating a new guild. Here's the routes...
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
But when I attempt to navigate to '/guild/create', I get a 404 because a guild with the slug "create" doesn't exist. How can I work around this particular issue?
Try put specific routes first:
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guil');

Laravel 5.4 - How to override route defined in a package?

I have created a package in Laravel 5.4 that sets up a basic backoffice. This package contains several routes that are using controllers from within the package. What I want to be able to do is to override the package defined routes in my application in order to plug custom controllers. For example, if I have a route
Route::get('login', [
'as' => 'admin.login',
'uses' => 'Auth\LoginController#showLoginForm'
]);
defined in my package that will use the Vendor\Package\Controllers\Auth\LoginController I want to defined a route for my application that will override that one and use the App\Controllers\Auth\LoginController.
Doing the obvious approach of defining the route in app routes files fails for the app routes files are run before the package routes file, so the package definition will prevail.
Is there any way to accomplish something of this kind?
I also tried to get the particular route in the RouteServiceProvider and use the method uses to set the controller that should be used to resolve it, like this
public function boot()
{
parent::boot();
Route::get('admin.login')->uses('App\Http\Controllers\Admin\Auth\LoginController#showLoginForm');
}
but this also fails to accomplish what is pretended.
Any clues on what I am doing wrong?
In config/app.php in the providers array put the service provider of the package before App\Providers\RouteServiceProvider::class, and then in your web.php routes you'll be able to override it with your custom route.
EDIT
For Laravel package auto discovery you can disable the package being auto discovered in your composer.json like this:
"extra": {
"laravel": {
"dont-discover": [
"barryvdh/laravel-debugbar"
]
}
},
In this example the barryvdh/laravel-debugbar package won't be autodiscovered, which means you would have to manually include its service provider in the config array and then you'll be able to rearrange your custom provider in the desired order.
Another option -- which doesn't have to muck with the order of service providers -- is to add a binding for the controller.
So e.g. in AppServiceProvider,
$this->app->bind(
\Vendor\Package\Controllers\Auth\LoginController::class,
App\Controllers\Auth\LoginController::class
);
You'll have to match controller method names, but you're doing that already in your example.
(Caveat on this answer: I haven't tested it in Laravel 5.4, but I just did this in Laravel 6.0 using the $bindings property which was added in Laravel 5.6. That said, this should be correct 5.4 syntax for doing the same thing).
Edit: For Laravel 6+ you can instead add the binding to the bindings array in AppServiceProvider:
public $bindings = [
\Vendor\Package\Controllers\Auth\LoginController::class =>
App\Controllers\Auth\LoginController::class,
// other bindings
]

Catching all other routes in Laravel 5.2

I am trying to send all undefined routes in a specific controller like this:
Route::get('/{slug:[A-Za-z0-9:/]+}', ['uses' => '\Site\Http\Controllers\AppController#index'])
It works in Lumen 5.1 but no chance in Laravel 5.2.
How can I setup such general match routes?
I didn't test it, but something like this should work:
Route::get('/{slug}', 'AppController#index')->where('slug', '([A-Za-z0-9:/]+)');
Put this after all of the relevant other routes:
Route::get('/{slug}', '\Site\Http\Controllers\AppController#index');
Route Undefined = 404 Error, so you can handle it properly.
Look at this question:
Redirect to homepage if route doesnt exist in Laravel 5

Laravel Routes: Use ::controllers and ::post

Within Laravel, I currently have:
Route::controllers([
'bla' => 'BlaController',
]);
But for a few specific methods within BlaController, I would like to pass parameters via the route (eg: /bla/parameter) and then be able to access that parameter within the controller.
However, declaring:
Route::get('bla/{parameter}', 'BlaController#exampleMethod');
keeps giving me a 404 not found when visiting it.
Is there some kind of conflict between using both declarations within routes.php and if so, is there a way to pass parameters to the controller for specific methods?
Edit: This is Laravel 5.1, if that makes any difference!
Can u try this ?
Route::get('/bla/{parameter}' ['uses'=>'BlaController#exampleMethod']);
http://laravel.io/forum/07-26-2014-routing-passing-parameters-to-controller

Categories