Viewed   106 times

Trying to play with Laravel today for the first time. I am getting the following error when I attempt to visit localhost/project/public:

InvalidArgumentException
Route [login] not defined.

app/routes.php:

<?php

Route::get('/', 'HomeController@redirect');
Route::get('login', 'LoginController@show');
Route::post('login', 'LoginController@do');
Route::get('dashboard', 'DashboardController@show');

app/controllers/HomeController.php:

<?php

class HomeController extends Controller {

    public function redirect()
    {
        if (Auth::check()) 
            return Redirect::route('dashboard');

        return Redirect::route('login');
    }

}

app/controllers/LoginContoller.php:

<?php

class LoginController extends Controller {

    public function show()
    {
        if (Auth::check()) 
            return Redirect::route('dashboard');

        return View::make('login');
    }

    public function do()
    {
        // do login
    }

}

app/controllers/DashboardController.php:

<?php

class DashboardController extends Controller {

    public function show()
    {
        if (Auth::guest()) 
            return Redirect::route('login');

        return View::make('dashboard');
    }

}

Why am I getting this error?

 Answers

3

You're trying to redirect to a named route whose name is login, but you have no routes with that name:

Route::post('login', [ 'as' => 'login', 'uses' => 'LoginController@do']);

The 'as' portion of the second parameter defines the name of the route. The first string parameter defines its route.

Monday, August 29, 2022
5

Did you enter above-mentioned URL directly in browser search bar? If you did its wrong way because you also need to enter API token with your request__!!

To check either request includes token or not make your own middleware.

Command to create Middleware

php artisan make:middleware CheckApiToken

https://laravel.com/docs/5.6/middleware

change middleware handle method to

public function handle($request, Closure $next)
{
    if(!empty(trim($request->input('api_token')))){

        $is_exists = User::where('id' , Auth::guard('api')->id())->exists();
        if($is_exists){
            return $next($request);
        }
    }
        return response()->json('Invalid Token', 401);
}

Like This Your Url should be like this

http://localhost:8000/api/todos?api_token=API_TOKEN_HERE

Monday, August 29, 2022
 
3

Make sure to get() the subpages as IlluminateSupportCollection and mapWithKeys() to reformat the results. Use toArray() to provide the format Nova assumes:

private function selectOptions(): array
{
    $subpages = DB::table('subpages')->get();
    return $subpages->mapWithKeys(function ($subpage) {
        return [$subpage->slug => $subpage->slug];
    })->toArray();
}

This is how the returned result should look like:

[
    'my-article-1' => 'my-article-1',
    'my-article-2' => 'my-article-2',
    'my-article-3' => 'my-article-3',
]
Thursday, September 1, 2022
 
talha_q
 
3

Decode to array and array_combine with the new keys.
Then loop the 'agent' and replace the keys again with array_combine.

$arr = json_decode($json, true);
$mainkeys = ["url", "secret_token", "agent"];
$subkeys = ["id", "quantity"];

$arr = array_combine(array_slice($mainkeys,0,count($arr)), $arr);

if(isset($arr["agent"])){
    foreach($arr["agent"] as &$val){
        $val = array_combine($subkeys, $val);
    }
}
unset($val); 

https://3v4l.org/mKePQ

array(3) {
  ["url"]=>
  string(26) "https://www.gosdoddgle.com"
  ["secret_token"]=>
  string(25) "stringstrinngstringstring"
  ["agent"]=>
  array(2) {
    [0]=>
    array(2) {
      ["id"]=>
      string(6) "sdsds1"
      ["quantity"]=>
      string(6) "sdsds1"
    }
    [1]=>
    &array(2) {
      ["id"]=>
      string(6) "sdsds1"
      ["quantity"]=>
      string(6) "sdsds1"
    }
  }
}
Tuesday, August 23, 2022
5

Looks like there's an issue with the the Controller Test Case not setting the default route, if custom routes have been set (this is a different behavior than the framework).

The patch from the issue:

/**
 * URL Helper
 * 
 * @param array $urlOptions
 * @param string $name
 * @param bool $reset
 * @param bool $encode
 */
public function url($urlOptions = array(), $name = null, $reset = false, $encode = true, $default = false)
{
    $frontController = $this->getFrontController();
    $router = $frontController->getRouter();
    if (!$router instanceof Zend_Controller_Router_Rewrite) {
        throw new Exception('This url helper utility function only works when the router is of type Zend_Controller_Router_Rewrite');
    }
    if ($default) {
        $router->addDefaultRoutes();
    }
    return $router->assemble($urlOptions, $name, $reset, $encode);
}

You'll need to pass true as the last argument when using the url() function.

Instead of patching the Test Case, you can just add the default routes someplace in the bootstrap process:

$frontController->getRouter()->addDefaultRoutes();
Thursday, September 1, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :