I have a big problem with session in Laravel 5.2. My session doesn't set in some route.
Like this
Route::post('add','SiteController#add');
This is my route.php:
Route::get('admin','AdminController#index');
Route::resource('admin/product','ProcuctController');
Route::resource('admin/news','NewsController');
Route::resource('admin/category','CategoryController');
Route::get('session','SiteController#session');
Route::post('add','SiteController#add');
Route::get('/{title}','SiteController#show');
Route::group(['middleware' => ['web']], function () {
Route::get('session','SiteController#session');
Route::post('add','SiteController#add');
});
My file shopping cms basket doesn't work. This is my SiteController function
public function add(Request $request)
{
if(session::has('cart'))
{
$cart=session::get('cart');
if(array_key_exists($request->product_id,$cart))
{
$cart[$request->product_id]++;
}
else
{
$cart[$request->product_id]=1;
}
session::put('cart',$cart);
//var_dump(session::get('cart'));
print 'ok';
}
else
{
$cart=array();
$cart[$request->product_id]=1;
session::put('cart',$cart);
var_dump(session::get('cart'));
}
}
Every time that I click the buy button, condition doesn't return true
In your router.php you register add and session route twice. one is inside of web middleware and another is outside. Remove the outside one. so your router.php will look like
Route::get('admin','AdminController#index');
Route::resource('admin/product','ProcuctController');
Route::resource('admin/news','NewsController');
Route::resource('admin/category','CategoryController');
Route::get('/{title}','SiteController#show');
Route::group(['middleware' => ['web']], function () {
Route::get('session','SiteController#session');
Route::post('add','SiteController#add');
});
And in your SiteController add this line in to top
use session;
Related
I designed a site with Laravel. now I want add new language to it.I read laravel document . It was good but I have a problem.suppose I have a page that show detail of products so I have a route like mysite.com/product/id that get product's id and show it.also I have a method in controller like
public function showProduct($id){
...
}
If I add new Language , the route will change to this: mysite/en/product/id
now I must change my method because now two parameter send my method.something like this :
public function showProduct($lang,$id){
...
}
So two problems arise:
I must change all method in my site which is time consuming
I do not need language parameter in methods because I set $locan via middleware
pay attention that I do not want remove for example en from my URL (because of SEO)
Open your RouteServiceProvider and say that language parameter actually is not a parameter, it's a global prefix.
protected function mapWebRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
'prefix' => Request::segment(1) // but also you need a middleware about that for making controls..
], function ($router) {
require base_path('routes/web.php');
});
}
here is sample language middleware, but it's need to be improve
public function handle($request, Closure $next)
{
$langSegment = $request->segment(1);
// no need for admin side right ?
if ($langSegment === "admin")
return $next($request);
// if it's home page, get language but if it's not supported, then fallback locale gonna run
if (is_null($langSegment)) {
app()->setLocale($request->getPreferredLanguage((config("app.locales"))));
return $next($request);
}
// if first segment is language parameter then go on
if (strlen($langSegment) == 2)
return $next($request);
else
// if it's not, then you may want to add locale language parameter or you may want to abort 404
return redirect(url(config("app.locale") . "/" . implode($request->segments())));
}
So in your controller, or in your routes. you don't have deal with language parameter
Something like
Route::group(['prefix' => 'en'], function () {
App::setLocale('en');
//Same routes pointing to the same methods...
});
Or
Route::group(['prefix' => 'en', 'middleware' => 'yourMiddleware'], function () {
//Same routes pointing to the same methods...
});
In my Laravel app, I want to delete an item and redirect immediately after that to the home page. Ideally, I want to do this purely in the routes file:
Route::get('event/{event}/delete', ['as' => 'events_delete', 'uses' => 'EventsController#destroy', function () {
return Redirect::route('events_list');
}] );
In my destroy function I have the following:
public function destroy(Event $event)
{
$event->delete();
}
The delete itself works (after refresh), but the redirect part does not seem to work. How can I redirect to the homepage immediately after the delete happened?
You either use the controller or the closure but not both, I don't think you are allowed to use them both.
But in your controller you can set the redirect like this:
public function destroy(RequestEvent $event)
{
$event->delete();
return redirect()->route('your_home_route');
}
I have an admin section in my app which works fine, however now I want to close it so only admin has access. Basically I want to redirect to admin/login if a non-admin person is trying to access admin pages.
This is my admin route (which works):
Route::controller('admin', 'AdminController');
I tried adding the following filter above the admin controller route, and I also commented out the local array element so the environment is set to production, but it is just not working.
Routes.php
Route::filter('isAdmin', function()
{
if ( ! Session::has('admin') ) {
return Redirect::to('admin/login');
}
});
Route::get('admin', array('before' => 'isAdmin', function()
{
echo 'You are over 200 years old!';
}));
Route::controller('admin', 'AdminController');
So if now I go to mysite/admin , I still get to admin dashboard even though session admin does not exist.
Something like this should work, this is untested
Filters.php
Route::filter('isAdmin', function()
{
if ( ! Session::has('admin'))
{
return Redirect::to('admin/login');
}
});
Routes.php
Route::group(['before' => 'isAdmin'], function()
{
Route::get('admin', 'AdminController#index');
});
AdminController.php
<?php
class AdminController
{
public function index()
{
echo 'they\'re an admin';
}
}
I currently have a booker and admin user groups. A booker user is assigned to 1 event, and an admin user is not assigned to any event.
Both user groups have route groups like this:
Route::group(['middleware' => 'booker'], function() {
Route::controller('event', 'EventController');
});
Route::group(['middleware' => 'admin'], function() {
Route::controller('admin', 'AdminController');
});
Actions in EventController are like this:
getIndex /event
getOverview /event/overview
postOverview /event/overview
getFacilities /event/facilities
postFacilties /event/facilities
etc.
When logged in as admin user group, is it possible for me to use the EventController actions for routes like this:
/admin/events/1
/admin/events/1/overview
/admin/events/1/facilties
/admin/events/1/schedule
etc.
Where, instead of getting the event id from the user, I would get it from the URL.
Thanks
Ok, I managed to get there, although it's quite messy so I'd still love to know if there is a nicer solution:
routes:
Route::group(['middleware' => 'booker'], function() {
Route::controller('event', 'EventController');
});
Route::group(['middleware' => 'admin'], function() {
Route::controller('admin/events/{id?}', 'EventController');
Route::controller('admin', 'AdminController');
Route::get('event', function() { return redirect('admin'); });
});
In EventController (using Sentry):
public function __construct()
{
$user = Sentry::getUser();
if($user->hasAccess('admin')) {
$id = Request::segment(3);
$event = Event::find($id);
if(!$event) {
abort(404);
}
} else {
$event = $user->event;
}
$this->data['event'] = $event;
}
public function getIndex()
{
return view('event.index', $this->data);
}
So as you can see I've had to use the URL segment for the id, as I couldn't find a way to pass the id variable in the route. The route variable just enables me to ignore the id and load the controller.
I found I couldn't use URLs like this within EventController views:
<a href="{{ URL::action('EventController#getOverview') }}">Overview<a>
// /event/overview
// /admin/events//overview
However using other helpers I could hack it in:
<a href="{{ URL::full().'/overview' }}">Overview<a>
// /event/overview
// /admin/events/3/overview
I am trying to create a filter that works for a specific domain and redirects a user from any page they try to visit if they have gone over a quota. So far, the code does not redirect at all.
Here is what I have so far in filters.php:
Route::filter('domain', function () {
if (stripos(Request::root(), Config::get('domains.main')) !== false) {
if (Auth::check()) {
$user = Auth::user();
$max_quota = Plan::where('id', $user->plan_id)->where('is_active', true)->pluck('max_quota');
$quota_used = Quota::where('user_id', $user->id)->count();
if (empty($max_quota)) {
return Redirect::to('account/error/inactive');
} elseif ($quota_used >= $max_quota) {
return Redirect::to('account/error/over_quota');
}
}
}
});
Even if I put this in routes.php under:
Route::group(['domain' => Config::get('domains.main')], function () {
Route::filter('*', function () { /* Same code here... */ });
}
It will then get into the filter function, successfully check the criteria, but the redirect still doesn't happen.
I think I am missing more than one key point here. Ideas?
To make this work, I needed to change:
Route::group(['domain' => Config::get('domains.main')], function () {
To:
Route::group(['domain' => Config::get('domains.main'), 'before' => 'domain'], function () {
I also needed to add redirect loop protection to this line:
if (Auth::check()) {
So that it becomes:
if (Auth::check() && !strpos(Request::fullUrl(), 'account/error')) {