InvalidArgumentException Route [account-activate] not defined - php

This is my route.php and I have also given the link of activate account. Then also "InvalidArgumentException
Route [account-activate] not defined." this error is coming:
<?php
Route::get('/',array(
'as'=>'home',
'uses'=>'HomeController#home'
));
//unauthenticated grp
Route::group(array('before'=>'guest'), function() {
//cross side request forgery protection
Route::group(array('before'=>'csrf'), function() {
//create acc(POST)
Route::post('/account/create',array(
'as'=>'account-create-post',
'uses'=>'AccountController#postCreate'
));
});
//create acc(GET)
Route::get('/account/create',array(
'as'=>'account-create',
'uses'=>'AccountController#getCreate'
));
});

You need to add a route to your routes.php file (located under app/) to define the 'account-activate' route that you are using to create the link in your e-mail.
If the function that will process the request is in the AccountController and it is called getAccountActivate then the route declaration will look like:
Route::get('/whateverUrlYouWant', [
'as' => 'account-activate',
'uses' => 'AccountController#getAccountActivate',
]);
You can use any URL, Controller and function. Put the URL you want to be associated with that route.

Related

How do I give route name to a closure route in Lumen?

Hi I have the following Lumen Route
$router->get('/end', function (\Illuminate\Http\Request $request) use ($router) {
$controller = $router->app->make('App\Http\Controllers\Legacy\EndLegacyController');
return $controller->index($request);
});
I am trying to give it a route name so that I can use redirect to redirect to this route like redirect()->route('name_of_route')
so far I have tried
})->namedRoute['end'] = '/end'; // No Effect
})->name('end') //Undefined function
but it didn't work
here's a list of present routes
Edit
Because of my certain requirement, I can't use ['as' => 'end', 'uses'=> 'ControllerName#Action']
you can use the following syntax: $router->get('/end', ['as'=>'name_here', function()]);

Call to a member function name() on null in laravel 5.4

When pressing my send button it's giving error like this-
Here is my routes web.php bellow-
Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {
Route::resource('message/send', 'MessageController#ajaxSendMessage')->name('message.new');
Route::delete('message/delete/{id}', 'MessageController#ajaxDeleteMessage')->name('message.delete');
});
Here is my controller MessageController.php bellow:
public function ajaxSendMessage(Request $request)
{
if ($request->ajax()) {
$rules = [
'message-data'=>'required',
'_id'=>'required'
];
$this->validate($request, $rules);
$body = $request->input('message-data');
$userId = $request->input('_id');
if ($message = Talk::sendMessageByUserId($userId, $body)) {
$html = view('ajax.newMessageHtml', compact('message'))->render();
return response()->json(['status'=>'success', 'html'=>$html], 200);
}
}
}
Resource routes should be named differently:
Route::prefix('ajax')->group(function () {
Route::resource('messages', 'MessageController', ['names' => [
'create' => 'message.new',
'destroy' => 'message.destroy',
]]);
});
Resource routes also point to a controller, instead of a specific method. In MessageController, you should add create and destroy methods.
More info at https://laravel.com/docs/5.4/controllers#restful-naming-resource-routes
You can't name a resource. Laravel by default name it, if you want to name all routes you must specify each one explicitly. It should be like this:
Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {
Route::get('message/send', 'MessageController#ajaxSendMessage')->name('message.new');
Route::delete('message/delete/{id}', 'MessageController#ajaxDeleteMessage')->name('message.delete');
});
Update
Another mistake of yours was trying to resource a single method. A Route::resource() is used to map all basic CRUD routes in Laravel by default. Therefore, you have to pass the base route and the class i.e:
<?php
Route::resource('message', 'MessageController');
Look at web.php line 28.
Whatever object you think has a name() method, hasn't been set, therefore you try and call a method on null.
Look before that line and see where it is (supposed to be) defined, and make sure it is set to what it should be!

Laravel routing to wrong controller method

I have following 2 routes :
Route1:
Route::get(config('api.basepath') . '{username}/{hash}/{object_id}', [
'action' => 'getObject',
'uses' => 'ObjectController#getObject',
]);
and
Route2:
Route::get(config('api.basepath') . 'object-boxes/{object_id}/boxes', function () {
if (Input::get('action') == 'filter') {
return App::call('App\Http\Controllers\FilterController#getFilteredContents');
} else {
return App::call('App\Http\Controllers\ObjectBoxController#show');
}
});
Now,
Route 1 is available with or without Auth middleware, while Route2 is under Auth middleware.
But, In either call, execution control is going to ObjectController#getObject
When I move Route1 below Route2, then everytime the call goes to ObjectBoxController#show.
I tried Preceed but nothing changed.
How should I fix this
Your first and second route are similar,
First Route
config('api.basepath') . '{username}/{hash}/{object_id}'
Second Route
config('api.basepath') . 'object-boxes/{object_id}/boxes'
when you have second route case, it has been treated as former one and takes username as object-boxes and object_id as boxes. So same function is called for both cases. So, try with something different route pattern for these two routes.
You should define what the variables in the routes are going to be, not the where at the end of each route:
Route::get(config('api.basepath') . '{username}/{hash}/{object_id}', [
'action' => 'getObject',
'uses' => 'ObjectController#getObject',
])->where('object_id', '[0-9]+');
Route::get(config('api.basepath') . 'object-boxes/{object_id}/boxes', function () {
if (Input::get('action') == 'filter') {
return App::call('App\Http\Controllers\FilterController#getFilteredContents');
} else {
return App::call('App\Http\Controllers\ObjectBoxController#show');
}
})->where('object_id', '[0-9]+');
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:
Read the documentation here

how to check if user is logged in by his session in route and then call controller method in laravel?

I'm using Laravel 5.2. I want to check user session in routes file, so that if session is set user can visit dashboard otherwise redirect to login page.
I have used following code for this but it's not working. It's not giving any error and not redirecting him to login page. anyhow if I write same code in controller functioin, it works fine.
Route::group(['middleware' => ['web']], function () {
Route::get('dashboard/index', ['uses' => 'DashboardController#index'], function() {
$value = $request->session()->get('name', 'not_loggin');
if ($value == 'not_loggin') {
return redirect('/user/login');
}
});
});
it also didn't worked if I write it in constructor.
You should use the auth middleware:
Route::get('dashboard/index', [
'middleware' => 'auth',
'uses' => 'DashboardController#index'
]);

Laravel error exception route not defined

I start a new laravel project make composed by two pages, I created the pages under the app/view directory and this is my route.php file:
Route::get('/', function()
{
return View::make('hello');
});
Route::get('welcome', function()
{
return View::make('welcome');
});
Route::any('signup', function()
{
return View::make('signup');
});
I can access to the pages signup by taping the link directly in the browser and also when I run artisan routes it shows me the routes that I created.
in the welcome.blade.php when I add the line
{{link_to_route('signup')}}
and reload the page I have this error
ErrorException
Route [signup] not defined. (View: C:\wamp\www\atot\app\views\welcome.blade.php)
how can I solve this problem?
Try this instead:
Route::any('signup', [
'as' => 'signup',
function() {
return View::make('signup');
}
]);
Your problem was that you didn't use a named route.
If you want, you can read more about it here: http://laravel.com/docs/routing#named-routes
Link_to_route is a method that generates a url to a given named route, so to make it work you can name each of your routes and then it will work
link_to_route('route.name', $title, $parameters = array(), $attributes = array());
In routes.php update the following
Route::get('/', array('as'=>'home', function()
{
return View::make('hello');
}));
Route::get('welcome', array('as'=>'welcome', function()
{
return View::make('welcome');
}));
Route::any('signup', array('as'=>'signup', function()
{
return View::make('signup');
}));
Then you can generate the following routes:
{{link_to_route('home')}}
{{link_to_route('welcome')}}
{{link_to_route('signup')}}
You should use either:
{{ link_to('signup') }}
Or declare the route using a name
Route::any('signup', array('as' => 'signup', function()
{
// ...
}));
The link_to_route helper function only works with a named route which accepts a route name in the first argument.

Categories