Laravel error exception route not defined - php

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.

Related

How to make sub-routes with prefix but can work without prefix in Laravel?

I'm building a multilingual Website using Laravel but I'm facing a problem about Locales.
I have 2 Languages for now (Ar/En) and my routes accept prefix to determine the Language.
I want my routes to be valid if not having a prefix and set a default Locale.
my current code is :
Route::group([
'prefix' => '/{locale?}',
'where' => ['locale' => '^(ar|en)$'],
'middleware' => ['setLocale']
], function(){
Route::get('/', function () {
return view('home');
});
Route::get('test', function (){
return 'test';
});
});
It works for the first route but for any sub-routes its not working if prefix is not provided!
You could define a fallback route
Route::fallback(function () {
abort_if(in_array(request()->segment(1), ['ar', 'en']), 404);
return redirect()->to(url(app()->getLocale().request()->getPathInfo()));
});

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()]);

No hint path defined for [voyager ]

I'm trying to create a custom admin page and Menu in Laravel Voyager.
This is the error that I'm getting.
ErrorException (E_ERROR)
No hint path defined for [voyager ]. (View: /Users/jake/code/DS/resources/views/vendor/voyager/orders/order.blade.php)
This is my web.php file
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
});
Route::get('/admin/orders', function () {
return view('vendor/voyager/orders/order');
});
This is my resources/views/vendor/voyager/orders/order.php
#extends ('voyager::master')
#section('content')
<h1>Hello There</h1>
#stop
When I add TCG\Voyager\VoyagerServiceProvider::class, to app.php it displays the page but other things like $dataTypeContent don't work. I'm using Laravel 5.6 whereby I thought Voyager is auto-discovered. Am I doing something wrong?
Any Help would be great.
Thanks, Jake.
I believe the error is coming from this route:
Route::get('/admin/orders', function () {
return view('vendor/voyager/orders/order');
});
Try changing the return to:
return view('voyager::orders.order');
Another thing, resources/views/vendor/voyager/orders/order.php is missing the blade portion, so resources/views/vendor/voyager/orders/order.blade.php

How to Map new route to specific old route in Laravel?

I have following route group for admin panel
Route::prefix('admin')->group(function (){
.
.
.}
I want to wrap this route to a new route e.g asda12asda
so that old behavior :
/admin/users
is changed to :
/asda12asda/users
not allowing old route. I don't want to change it internally from the system and want to find some efficient Laravel way to achieve it.
Redirect the old route to a new route
Route::prefix('admin')->group(function (){
Route::any('login', function () {
// Redirect to new route
redirect()->route('new route');
});
});
by creating a new route and mapping it accordingly
Route::prefix('asda12asda')->group(function () {
Route::any('login', function () {
// Do whatever you were about to do
})->name('new route');
});
If you are using Laravel 5.4 then you can add a new route file. Assume your route name is
adsp.php then add it to RouteServiceProvider.php like this.
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/adsp.php.php');
});
}

Skipping a middleware for a particular route inside a route group in Laravel

I Want to skip the middleware for a particular route in a route group. How can I do this?
Route::group(['prefix' => 'testgroup','middleware' => ['login.oauth']],function (){
Route :: get('/', 'testController#index');
Route :: get('/api','testController#apiCall');
});
I want to skip the 'login.oauth' middleware for the Route :: get('/api','testController#apiCall')
Please keep that testgroup function must be accessible to all routes and middleware function to particular(some other route) in the same function
Route::group(['prefix' => 'testgroup'], function () {
Route::group(['middleware' => ['login.oauth'], function() {
Route :: get('/', 'testController#index');
});
Route :: get('/api','testController#apiCall');
});
Just create a second group without the middleware:
Route::group(['prefix' => 'testgroup','middleware' => ['login.oauth']],function (){
Route :: get('/', 'testController#index');
});
Route::group(['prefix' => 'testgroup'],function (){
Route :: get('/api','testController#apiCall');
});

Categories