As mentioned in the title I'm using Laravel 5.2 and I've set up TwigBridge so I can use twig for my views. I'm just using the basic Auth package that comes with Laravel. The problem is when I use twig templates I get the following error:
An exception has been thrown during the rendering of a template ("Session store not set on request.") in "...resources/views/auth/register.twig" at line 17.
This is pointing to a use of the {{ old('name') }} call. I've tried switching it to input_old as I think TwigBridge prefers that, but that didn't help. If I use the blade template there's no problem though. I'm not doing anything special either. I just rename the blade template so it isn't called, and my register twig template is called instead since it uses the name register.
The old values are fetch from the session. To get these values to store in session & fetch these values back, you need to apply \Illuminate\Session\Middleware\StartSession and
\Illuminate\View\Middleware\ShareErrorsFromSession middlewares to your routes. Both of these are a part of middleware group web.
You can apply web middleware group to all your routes(unless you are creating an API), like this:
Route::group(['middleware' => ['web']], function () {
Route::get("login" , "AuthController#getLogin");
//Similarly all other routes here
});
Related
I'm using same route name for the get & post methods in route. those routes are using for the same purpose.
ex : I'm calling to load add view form using get route
Route::get('add', 'UserController#addView')->name('user.add');
then,
I'm calling to store data in that form using post route
Route::post('add', 'UserController#store')->name('user.add');
is there any issue , If I use the same route name like this?
No, you can not use the same name for 2 different routes as is stated in the documentation if you really need to name the routes you should look for different names, but if there's no need to have named routes you can have each url with its method like:
Route::get('/your-url', 'App\Http\Controllers\UserController#addView');
Route::post('/your-url', 'App\Http\Controllers\UserController#store');
If you are making a CRUD you can have:
Route::resource('user', UserController::class);
This will create all the urls needed for a CRUD:
Actually you have same name for both routes i.e., name('user.add'). Change that.
Yes you can get issues in future. For example, I had issues after functionality update - I've updated route interface (added some fields) and new fields does not appeared in result after opening URL.
Please run command php artisan optimize to see is it all ok.
I needed to pass a subdomain name to all views so that when they generate routes with route('namedRoute') users will end up in the same subdomain.
I ended up creating a ViewServiceProvider which registers a view composer where I get the subdomain from the request like so:
use Illuminate\Support\Facades\View as FView;
use Illuminate\View\View;
class ViewServiceProvider extends ServiceProvider {
FView::composer('*', function(View $view){
$view->with('subdomain', request()->route()->subdomain);
});
}
This way the subdomain variable will be passed to every singe view every time thanks to the '*' as documented in Laravel docs.
Then when I need to generate any route in any view, I will always have to pass the subdomain like so and the route will be generated correctly.
{{ route('signInPage', ['subdomain' => $subdomain]) }}
So is there something in laravel (like a post view processing) that I can hook into to populate the subdomain automatically so I don't have to now modify every route generation in every view?
The URL generator can take defaults so you don't have to pass a paremeter for the route when generating the URL:
URL::defaults(['subdomain' => ....]);
You can create a route middleware that gets the subdomain parameter from the request and sets this default.
Laravel 8.x Docs - URLs - Default Values
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
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');
according to the Laravel 5.5 docs, there is a named() method for accessing route names:
if ($request->route()->named('profile')) {
//
}
Inspecting the source, I learned that this named method simply fetches the 'as' property of the action object:
$this->action['as']
My problem is I'm stuck using Laravel 5.2, which doesn't have a named() method. I can't use route()->action['as'] in my blade template, because the actionobject is protected. Is there an equivalent getter method in 5.2, to check the name of the current route? I want to flow control in my blade.php file like this:
#if(route()->action['as'] == 'blog.edit')
//
#endif
Maybe I missed it, but I didn't see anything in the Laravel 5.2 docs: https://laravel.com/docs/5.2/routing#named-routes
I succeeded in checking the route using
#if(request()->is('blog/add'))
//
#endif
But that is using the route URI. I prefer to use the route name instead as it's less clunky
answer from Gitter courtesy of Ben Johnson:
#if(Route::currentRouteName() == 'blog.edit')
//
#endif