I'm passing these params from firstsite.com
param1 = value
param2 = value
to
secondsite.com
Like this
http://secondsite.com/pay?param1=value¶m2=value
But the pay.blade.php is under of middleware
where pay.blade.php accessible only for authenticated users.
and I want to redirect them to http://secondsite.com/login?param1=value¶m2=value
where params is still exists.
But in my case the params of course disappears and redirect to login.blade.php since it is stated on my web.php routes.
Here's my route
Route::group(['middleware'=> ['auth']], function(){
Route::get('/pay', 'PayController#index');
});
PayController.php
public function index()
{
if(session()->get('merchId') == "")
{
abort(404);
}
else
{
return view('pay');
}
}
But as of now I'm declaring abort(404) and will revise it once I'm done with my objective
Is it possible to keep those params going to login.blade.php?
Related
I'm trying to make a button appear when a user log in. If the user isn't log in, the button wouldn't appear on the page. So the first thing i need is to determine if the user has log in or not. The problem is, The Auth::check() keeps returning null. I've done research that we should put the route inside the Auth middleware, but in doing so, users would have to log in to see the page and i don't want that. I want the users to be able to see the page without login and when they do login, a button will appear on the same page.
Component
<button v-if="showBtnDelete==true" class="btn btn-danger">Delete</button>
<script>
getUserType(){
axios.get('api/gallery/getUserType').then(res=>{
this.showBtnDelete = res.data;});
},
</script>
Controller
public function getUserType()
{
if(Auth::check()){
return 'true';
}else{
return 'false';
}
}
Web.php
Route::group(['middleware' => ['auth']],function(){
Route::get('/add-image', function () {return view('layouts.master');});
});
Auth::routes();
Route::get('/logout','Auth\LoginController#logout');
Route::get('/', function () {
return view('home');
});
Route::get('{path?}', function () {
return view('layouts.master');
})->where('path', '[\/\w\.-]*');
guard API or web
Auth::guard('api')->check();
Auth::guard('web')->check()
You're returning 'true' and 'false' as strings which both returns true when non strictly compared and false when strictly compared
echo 'false' == true; // true
echo 'false' === true; // false (not same data type)
And vice versa...
Now Auth::check() naturally returns a boolean so the if, else statement is redundant all together...
You can use the helper method to use each guard when needed
public function getUserType()
{
return auth()->check();
}
If anyone has the same problem. The problem is that your route is not protected by the middleware group web.
That means it will not be able to retrieve the session of the current user so the Auth facade will always return null.
Add middleware('web') to the route accessing the method (code) and you will be able to retrieve the current user if logged in.
For now in Laravel i am testing url and in route i have
Route::group(['prefix' => 'game'], function (){
Route::get('/location/{location?}/action/{action?}/screen/{screen?}','GameController#index')->name('game.index');
});
In controller when i wanna pass params i have to type
example.com/game/location/1/action/update/screen/main
if i wanna pass only location and screen i have an error cause in url second param should be action.
I can create url like
example.com/game/?location=1&screen=main
and controller $request->screen and location works fine. But is any way to not using & ? and do this like:
example.com/game/location/1/screen/main
For this route
Route::get('/locations/{location?}/actions/{action?}/screens/{screen?}','GameController#index')->name('locations.actions.screens.show');
In GameController index method, you can get these parameter as
public function index(Location $location, Action $action, Screen $screen) {
// here you can use those models
}
if you are using route-model binding,
if not using
public function index($location, $action, $screen) {
// here you can use these variables
}
If the route name is locations.actions.screens.show then in view, it will be
Test
Now, if you have some query parameter
then it will be like $http://example.com/?test="some test data"&another_test="another test"
you can access these parameter like
public function myfunction(Request $request) {
dd($request->all());
}
Let's consider you want to retrieve all games, that belongs to a particular screen which belongs to a particular action and that belongs to a particular location, what your urls seems to be in your question, in that case, the url will be
Route::group(['prefix' => 'game'], function (){
Route::get('locations/{location?}/actions/{action?}/screens/{screen?}','GameController#index')->name('game.index');
});
url seems to be game/locations/1/actions/1/screens/1 where action and screen parameter can be optional
now in your controller index() method
public function index(Location $location, Action $action=null, Screen $screen=null) {
//using the model instance you received, you can retrieve your games here
}
Your error makes sense
URL second param should be action
because your route is with wild card location, action, screen
Route::group(['prefix' => 'game'], function (){
Route::get('/location/{location?}/action/{action?}/screen/{screen?}','GameController#index')->name('game.index');
});
To access this route you have to generate a URL with a wild card like
example.com/game/location/1/screen/main
and example.com/game/?location=1&screen=main not going to work because of your route URL and you can't access like $request->screen.
so your controller must be like
public function index($reuest, $location, $action, $screen){
}
You can directly access $location, $action, $screen and if you request something like
example.com/game/location/1/screen/main?param1=1¶m2=2
Those are accessible through request like
$request->param1 and $request->param2
Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:
Route::get('user/{name?}', function ($name = null) {
return $name;
});
you can use Pattern Based Filters
You may also specify that a filter applies to an entire set of routes based on their URI.
Route::filter('admin', function()
{
//
});
Route::when('admin/*', 'admin');
I am using Laravel framework. There is a function in controller that creates session with name store_id
StoreController.php
function initiate($id)
{
//Some queries
session['store_id' => 'some value'];
}
Now if I run this function on one tab then session::get('store_id') is going on. But if I open another tab in the same browser then again run the function that means session('store_id') will be again set. How do I handle this situation that if there is already a session then it should redirect to it's perspective url.
Okay first of all, Bruuuhhhh been there and done that
Alright, lets begin. you want that if there is already a session with store_id going on then you want user to redirect or send back.
In your controller add this
public function initiate()
{
if(session()->has('store_id'))
{
//What ever your logic
}
else
{
redirect()->to('/store')->withErrors(['check' => "You have session activated for here!."]);
}
}
Most probably you would be wondering that user can just go to other url after /store/other-urls Yess he can.
To avoid this. Add a custom middleware
php artisan make:middleware SessionOfStore //You can name it anything.
In that middleware
public function handle($request, Closure $next)
{
if($request->session()->has('store_id'))
{
return $next($request);
}
else
{
return redirect()->back()->withErrors(['privilege_check' => "You are not privileged to go there!."]);
}
return '/home';
}
in your main store page. Add an anchor tag Stop Service
Now in your web.php
Route::group(['middleware' => 'SessionOfStore'], function()
{
//Add your routes here.
Route::get('/stop', 'StoreController#flushSession');
});
Now you have restrict access to urls and has checked the sessions.
Now in
public function flushSession()
{
//empty out the session and
return redirect()->to('/home');
}
The Laravel session helper has the function has to check this.
if (session()->has('store_id'))
{
// Redirect to the store
}
else
{
// Set the store id
}
The documentation contains all of the possible functions that can be used with the session helper.
I want to redirect 2 different page from this controller function along with value.Here is my code. It works but both of time url become same.what shuld I do?
//in routes.php
Route::post('/','mycontroller#check');
// in controller.php
public function check(Request $request)
{
$c_email = $request->email;
$c_pass=$request->pass;
$c_type=$request->select;
$var=DB::select("SELECT * FROM reg where email = '$c_email' and Password = '$c_pass' and type = '$c_type'");
if ($var) {
return view('farmer')->with('user',$var);
// return redirect('farmer')->with('user',$var);
}
else {
$msg="Invalid login";
return view('index')->with('show',$msg);
}
}
If you want to actually redirect u can use the redirect() helper as statet in the official docs https://laravel.com/docs/5.3/redirects
You can also pass data
redirect('/my-route')->with(['user' => $var]);
The passed data can then be accesses through the session helper
$var = session('user')
HOWEVER, it seems like you have major issues in your code. Your password does not seem to be encrypted. Also there's no reason to use plain sql instead of eloquent here.
The route that is shown in the browser is defined in your
Route::post('/','mycontroller#check');
If you just return different views, the route does not change. You need to redirect to other views.
If you redirect to other routes you will ofcourse need to add / define them.
Route::get('/my-route', function() {}); // or post etc.
I'm currently trying to route as follows:
If user GETs /account/
If session has account_id, user is logged in; show his account information
If not, user is not logged in; show login/create form
If user POSTs /account/
If input has create, user wants to create account; create it
If not, user wants to login; find his account and go again to /account/
My routes are set this way:
Route::get('account', function() {
if (Session::has('account_id'))
return 'AccountsController#show';
else
return 'AccountsController#index';
});
Route::post('account', function() {
if (Input::has('create')) {
return 'AccountsController#create';
else
return 'AccountsController#login';
)};
This is somewhat how I would do with Rails, but I don't know how to point to the controller method. I just get the returned string as a result. I didn't find it in Laravel documentation (which I found really poor, or have I searched wrong?) neither in any other web tutorial.
Try the following:
Route::get('account', function() {
if (Session::has('account_id')) {
$action = 'show';
return App::make('AccountsController')->$action();
}
else {
$action = 'index';
return App::make('AccountsController')->$action();
}
});
Route::post('account', function() {
if (Input::has('create')) {
$action = 'create';
return App::make('AccountsController')->$action();
}
else {
$action = 'login';
return App::make('AccountsController')->$action();
}
)};
So, you want to put all your logic in the controllers.
You would want to do
Route::get('account', 'AccountsController#someFunction');
Route::get('account', 'AccountsController#anotherFunction');
Then, in the respective controllers, you would want to make your tests, then do, for example,
if (Session::has('account_id')){
return Redirect::action('AccountsController#show');
}
else{
return Redirect::action('AccountsController#index');
}
However, make sure you define a route for each action, so your application can detect a URL.
For example, if you want to have
Redirect::action('AccountsController#show')
you will need to define this action like:
Route::get('/account/show', 'AccountsController#show')