Signed Route not defined laravel - laravel

I am testing Signed route.
im my resources >> views >> web.php i have my route two routes
Route::get('/unsubscribe/{user?}', function ($user='') {
return view('about');
});
Route::get('test', function () {
echo URL::signedRoute('unsubscribe', ['user' => 1]);
});
when i hit http://127.0.0.1:8000/test/ to test my Signed URL i am getting error
Route [unsubscribe] not defined.

Try this: (add name to the route)
Route::get('/unsubscribe/{user?}', function ($user='') {
return view('about');
})->name('unsubscribe');

Related

Laravel route group prefix - variable not working

in web.php :
Route::group(['middleware'=>['checklang','checkmoney']],function(){
Route::get('/', function () {
return redirect('/'.session()->get('lang'));
});
Route::group([
'prefix' => '{locale}',
'where'=>['locale'=>'[a-zA-Z]{2}']],
function() {
Route::get('/tour/{id}','HomeController#getTours');
});
});
in HomeContoller :
public function getTours($id){
dd($id);
}
when trying to access url : example.com/en/tour/5
getting result
en , but should be 5
Where is a problem and how to solve it?
Your route has 2 variables, {locale} and {id}, but your Controller method is only referencing one of them. You need to use both:
web.php:
Route::group(['prefix' => '{locale}'], function () {
...
Route::get('/tour/{id}', 'HomeController#getTours');
});
HomeController.php
public function getTours($locale, $id) {
dd($locale, $id); // 'en', 5
}
Note: The order of definition matters; {locale} (en) comes before {id} 5, so make sure you define them in the correct order.

Laravel pass subdomain as a value to another route

now I have this code
Route::group(['domain' => '{subdomain}.'.env('DOMAIN')], function()
{
Route::any('/', function($subdomain)
{
Route::get('{/subdomain}/{identifier}/{reCreate?}','AController#index');
});
});
I want to pass {subdomain} to Route::get('{/subdomain}/{identifier}/{reCreate?}','AController#index'); for when call subdomain.localhost/identifier I call AController#index I should pass the value of subdomain to AController#index
I think by using Route::input('subdomain'); you can access the subdomain parameter.
Inside AController:
public function index() {
dd(Route::input('subdomain'));
}
Please try this.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
I figured out the answer thanks guys for your help ,
the answer is just call the route inside route::group
Route::group(['domain' => '{subdomain}.'.env('DOMAIN')], function()
{
Route::any('/{identifier}/{reCreate?}','AController#index');
});
and inside Controller function index I can access the value as normal function($subdomain)

Laravel subdomains route and controller parametrs

I have question.
My routes is
Route::group(['domain' => '{account}.'.$domain], function () {
Route::get('/confirmmail/{hash}', 'Auth\RegisterController#confirmEmail');
});
And my controller method is
public function confirmEmail($domain, $hash)
{
$user = User::where('confirm_token', $hash)->firstOrFail();
$affectedRows = $user->update(array('active' => 1));
if ($affectedRows) {
Auth::login($user);
return Redirect::to('http://'.$user->workshop->slug.'.'.config('app.domain'));
} else {
echo "nie";
}
}
In my method I must use two parametrs $domain and $hash, how I must change route to have
function confirmEmail($hash)
You can access the route parameters from the request helper
request()->route('domain')
request()->route('hash')

How to call function in __constructor using laravel 5?

I am fetching menus from database based on user rights and displaying it to my web page but if i access any url whose access i don't have then too it opens that page.
For this i have created and called access_denied function which redirect user's home page.
I have called access_denied function from constructor of AuthController because AuthController gets loaded on each page.
I have used following code
AuthController
public function __construct()
{
$this->accessDenied();
}
public function accessDenied()
{
$url_segment1 = Request::segment(1);
$url_segment2 = Request::segment(2);
$url_segment = $url_segment1 . '/' . $url_segment2;
$user_data = Auth::user()->toArray();
$dadmin = array_keys($user_data['admin']);
//this is sample of array
// $user_data['admin'] => Array
// (
// [admin/roles] => 1
// )
if (!in_array($url_segment, $dadmin)) {
return redirect('/home');
}
}
But I am getting following error
Non-static method Illuminate\Http\Request::segment() should not be called statically, assuming $this from incompatible context
If i using incorrect process then please suggest me correct way to redirect unauthorised user on home page.
First, you should create a middleware. In a command prompt type:
php artisan make:middleware AccessDenyMiddleware
Then you go to app/Http/Middleware/AccessDenyMiddleware.php and fill in the handle function {your own code}
$url_segment1 = Request::segment(1);
$url_segment2 = Request::segment(2);
$url_segment = $url_segment1 . '/' . $url_segment2;
$user_data = Auth::user()->toArray();
$dadmin = array_keys($user_data['admin']);
//this is sample of array
// $user_data['admin'] => Array
// (
// [admin/roles] => 1
// )
if (!in_array($url_segment, $dadmin)) {
return redirect('/home');
}
But add the following line
return $next($request); // If passed, proceed with the route
Then, in a route, you should type:
Route::get('/yoururlhere', ['middleware' => 'AccessDenyMiddleware', function() { /* Put your work here */ } ]);
There are much better approaches. Like Authorisation if you are using Laravel 5.2
Or maybe change the default Authenticate middleware if you are using Laravel 5
You can use a middleware for thath https://laravel.com/docs/5.2/middleware#introduction.
php artisan make:middleware RoleRouteMiddleware
You should put thath code in the handle method of the middleware "App\Http\Middleware\RoleRouteMiddleware" and use the $request variable instead of the facade Request.
The middleware would filter earch request to your app.
Register a middleware, add it on app/Http/Kernel.php at routeMiddleware array
protected $routeMiddleware = [
....
'alias' => App\Http\Middleware\RoleRouteMiddleware::class,
];
and then use it on specific routes like this:
Route::get('admin/profile', ['middleware' => 'alias', function () {
//your code here
}]);
or in route gruoups:
Route::group(['middleware' => 'alias', function () {
//your filtered routes
}]);

get current path outside of Route

In this URL: http://siteName/html/test
I want to catch test, of course inside the route.
This works great:
Route::get('test', function()
{
return Route::getCurrentRoute()->getPath();
});
But I want to access the path outside of any route and in my route.php file like this:
// routes.php
return Route::getCurrentRoute()->getPath();
Route::get('test', function()
{
...
});
In case you try to catch all routes. Add this as your last route.
Route::any('{all}', function($uri)
{
return Route::getCurrentRoute()->getPath();
})->where('all', '.*');

Resources