I'm trying to implement a simple CMS with Laravel 5.2 which basically handles two kinds of routes. The first one is used to browse a website view, that has to be {view}.html. The controller iterates the database records and if it can't find that page, will return a 404 error page:
Route::get('/{page}', [
'as' => 'page',
'uses' => 'Website\WebsiteController#showPage'
])->where(['page' => '.+(\.html)']);
For example these routes will match:
www.mydomain.ext/homepage.html
www.mydomain.ext/about.html
www.mydomain.ext/news.html
www.mydomain.ext/contact.html
and so on. The second one is a route group for the admin control panel:
Route::group([
'prefix' => env('ADMIN_PREFIX', 'admin'),
'as' => env('ADMIN_PREFIX', 'admin') . '::',
'middleware' => ['auth']
], function() {
/*
* ADMIN ROUTES
*/
});
So all the routes in this group will be something like:
www.mydomain.ext/admin/dashboard
www.mydomain.ext/admin/user/1
www.mydomain.ext/admin/page/2
and so on.
From what I've found here:
Laravel matches routes from the top down. So all you need to do is put 'campaigns/add' above the wildcard route.
And that's what I've done:
routes.php
Route::group([
'prefix' => Localization::setLocale(),
'middleware' => ['localeSessionRedirect', 'localizationRedirect']
// LaravelLocalization (https://github.com/mcamara/laravel-localization)
], function() {
Route::auth();
// admin routes
Route::group([
'prefix' => env('ADMIN_PREFIX', 'admin'),
'as' => env('ADMIN_PREFIX', 'admin') . '::',
'middleware' => ['auth']
], function() {
/*
* ADMIN ROUTES
*/
});
Route::get('/{page}', [
'as' => 'page',
'uses' => 'Website\WebsiteController#showPage'
])->where(['page' => '.+(\.html)']);
});
But when I try to call an admin route, Laravel throws this error:
Missing argument 1 for App\Http\Controllers\Website\Core\WebsiteCoreController::showPage()
So I suppose I'm doing something wrong... Any suggestions on how to fix my code?
Thanks everyone in advance
Related
i just want to group all my admin routes in my laravel. I'm a beginner in laravel and i want to synchronize all my admin routes in one group, my question is, why i cant put the post route inside the group of my admin routes?
Here is my routes:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
});
my above code was returning error , where laravel says admin.login route doesn't exist. Then i tried to put the post route outside the group and it works. Why?.
Here is the code where returns no error:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
});
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
Because you use as in your route group and it's admin:: and you may link to admin.
Now it goes to admin::login and you need admin.login
I am working on adding a comment like stackoverflow using chatter discussion package by devdojo so here is the problem I am writing code to show comments but an undefined variable error is coming up.
Error Page ScreenShot
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
return view('chatter::discussion', compact('chatterreplies'));
echo "<pre>"; print_r('$chatterreplies'); die;
}
In Web.php route is
/*
* Post routes.
*/
Route::group([
'as' => 'posts.',
'prefix' => $route('post', 'posts'),
], function () use ($middleware, $authMiddleware) {
// All posts view.
Route::get('/', [
'as' => 'index',
'uses' => 'ChatterPostController#index',
'middleware' => $middleware('post.index'),
]);
// Create post view.
Route::get('create', [
'as' => 'create',
'uses' => 'ChatterPostController#create',
'middleware' => $authMiddleware('post.create'),
]);
// Store post action.
Route::post('/', [
'as' => 'store',
'uses' => 'ChatterPostController#store',
'middleware' => $authMiddleware('post.store'),
]);
//Adding Comments
Route::post('/reply/{id}', [
'as' => 'store',
'uses' => 'ChatterreplyController#store',
'middleware' => $authMiddleware('post.reply.store'),
]);
//showing Comment
Route::get('/reply/{id}', [
'as' => 'show',
'uses' => 'ChatterreplyController#show',
'middleware' => $middleware('post.show'),
]);
First, I would suggest putting your debugging statements (...print_r...) before the return statement in your controller action, this way :
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
echo "<pre>"; print_r('$chatterreplies'); die();
// or use the laravel helper
dd($chatterreplies)
return view('chatter::discussion', compact('chatterreplies'));
}
You should see the content of the $chatterreplies variable.
If this is ok, check your controller name in web.php because it seems that it should be ChatterReplyController#show instead of ChatterreplyController#show (is the R letter in ChatterreplyController#show capital or not ? ) if you're following the camelCase convention as in ChatterPostController#store for instance.
I defined two different guard in auth.php file like this :
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'website' => [
'driver' => 'session',
'provider' => 'websites',
]
]
There is two different section routes in web.php routes. one for website admn and another for normal users that is a member of those websites. like this :
Route::prefix('/website/{website}')->middleware('auth:website')->group(function () {
Route::group(['prefix' => 'banner_ads'], function () {
Route::get('banner_adsDatatable', ['as' => 'banner_adsDatatable', 'uses' => 'BannerAdsController#banner_adsDatatable']);
});
Route::resource('/banner_ads', 'BannerAdsController');
Route::prefix('/member/{member}')->middleware('auth:web')->group(function () {
Route::group(['prefix' => 'banner_ads'], function () {
Route::get('banner_adsDatatable', ['as' => 'banner_adsDatatable', 'uses' => 'BannerAdsController#banner_adsDatatable']);
});
Route::resource('/banner_ads', 'BannerAdsController');
});
});
Problem is that I have a resource controller (banner ads) that is shared with to users (website admins and members). As you can see I must to add it twice time.
But beacause for normal user I defined banner ads controller again, when I call banner_ads.update for example always return unAuthenticated user.
I do not know what can I do for solve this problem.
Seems like your auth:web middleware is inside auth:website. This is basically checking for an authenticated admin who is trying to access routes of an authenticated user.
You are trying to access routes as a normal user. So it is not working. Just replace your code with this:
Route::prefix('/website/{website}')->middleware('auth:website')->group(function () {
Route::group(['prefix' => 'banner_ads'], function () {
Route::get('banner_adsDatatable', ['as' => 'banner_adsDatatable', 'uses' => 'BannerAdsController#banner_adsDatatable']);
});
Route::resource('/banner_ads', 'BannerAdsController');
});
Route::prefix('/member/{member}')->middleware('auth:web')->group(function () {
Route::group(['prefix' => 'banner_ads'], function () {
Route::get('banner_adsDatatable', ['as' => 'banner_adsDatatable', 'uses' => 'BannerAdsController#banner_adsDatatable']);
});
Route::resource('/banner_ads', 'BannerAdsController');
});
Thanks for any help you can provide. I am 10 days into learning Laravel 5.1 so any suggestions on what I have missed will be greatly appreciated!
I am having an issue with the resolution of named routes in Laravel 5.1. I am building an app that will have a URL in the format of {organisation}.website.com, where {organisation} is defined at registration of customer.
The code routes perfectly when using sample subdomains, so long as I hardcode the route address (e.g.: redirect('/home');), but when I try and route by named routes from Controller (e.g.: redirect()->route('session.create');) the routes resolve like this:
http://%7Borganisation%7D.website.com/home
My routes look like this:
<?php
/**
* Entity routes - resolves {organisation}.website.com
*/
Route::group([
'domain' => '{organisation}.' . env('APP_DOMAIN')
], function(){
/*
|----------------------------------------------------------------------
| Freely available routes for login, registration, password reset etc
|----------------------------------------------------------------------
*/
Route::group([
'middleware' => 'guest'
], function(){
// Login
Route::get('login', ['uses' => 'SessionController#create', 'as' => 'session.create']);
Route::post('login', ['uses' => 'SessionController#store', 'as' => 'session.check']);
});
Route::group([
'namespace' => 'Website',
'middleware' => ['authorise'],
], function(){
});
/*
|-----------------------------------------------------------------------
| Potentially secured routes
|-----------------------------------------------------------------------
*/
Route::group([
'middleware' => ['authorise']
], function(){
// Logout and destroy all Auth data
Route::get('logout', ['uses' => 'SessionController#destroy', 'as' => 'session.destroy']);
});
});
In my Controllers I call the routing like this:
return redirect()
->route('session.create')
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
and it successfully completes, but with the above URL and 404. If I change to this it works perfectly.
return redirect('/home')
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
What have I missed in my organisation sub domain set up to make named routes work? Thanks!
Got it working. Below is how I fixed the issue, which essentially I had to bind on the fly to the route the slug of the sub-domain. Hope this helps someone!
I first had to pass the Organisation class to my controller methods and change the route calls like this:
public function destroy(Organisation $organisation)
{
Auth::logout();
return redirect()->route('session.create', ['organisation' => $organisation->slug]);
}
I have these three Routes
Route::get('login', [
'uses' => 'loginController#showLogin',
'as' => 'login.show',
'before' => 'guest'
]);
Route::get('dashboard', [
'uses' => 'DashboardController#index',
'as' => 'dashboard.show'
]);
Route::filter('guest', function()
{
if (Auth::check())
return Redirect::route('dashboard.show');
});
When I log-in the Auth:check() recognize it, but instead of redirecting me to localhost:8000/dashboard it redirects me only to localhost:8000
Am'I doing something wrong?
Thank you very much for any suggestions.
Redirect::route() will only redirect to saved routes. You have named your first route 'login.show'. Your route 'dashboard' is actually named 'dashboard.show'. You will need to do Redirect::route('dashboard.show')
If you are just wanting to redirect to a URL then do Redirect::to('dashboard')