I am trying to display some content in a partial only to logged in users. The content is never displayed. Here is the bit of code:
#if(Auth::check())
<a href="{{ route('backend.blog.edit', $id) }}">
Edit
</a>
#endif
The page that contains the partial is available to everyone; I only want to hide this one bit of content.
Can anyone point me in the right direction?
Additional info
This is part of a CMS system I am attempting to write. So, the controller is called via a service provider (RouteServiceProvider). I am pulling the routes based on a pages model. Here is the pertinent method:
public function map(Router $router)
{
$this->mapWebRoutes($router);
foreach (Page::all() as $page) {
$router->get($page->uri, ['middleware' => 'web', 'as' => $page->name, function () use ($page, $router) {
return $this->app->call('App\Http\Controllers\PageController#show', [
'page' => $page,
'parameters' => $router->current()->parameters()
]);
}]);
}
}
I found the solution
public function map(Router $router)
{
$this->mapApiRoutes();
$this->mapWebRoutes();
Route::group(['middleware' => 'web'], function ($router) {
if (! app()->runningInConsole()) {
foreach (ProductCategory::all() as $category) {
$router->get($category->uri, ['as' => $category->name, function () use ($category, $router) {
return $this->app->call(
'App\Http\Controllers\PageController#show', [
'category' => $category,
'parameters' => $router->current()->parameters()
]);
}]);
}
}
});
}
Related
I'm running this code to use my project policy for middlewares. It works as long as {project} is "last in the chain". Is there any way to make it work for deeper levels too?
Route::middleware(['web', 'auth:sanctum', 'verified'])->group(function () {
//...
Route::prefix('project/{project}')->middleware('can:view,project')->group(function () { // This works, but not if I go one more level after this...
Route::get('/', function (Project $project) {
return view('projects::show', [
'project' => $project,
]);
})->name('project');
Route::prefix('settings')->middleware('can:update,project')->group(function () {
// I get 403 here and I don't think I even get through the first middleware...
});
});
});
Either this
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::get('/', function (Project $project) {
return view('projects.show', [
'project' => $project,
]);
});
Route::group([
'prefix' => 'settings',
], function (Project $project) { // this is the one line I am not sure of
// some routes
})->can('update', Project::class);
})->can('view', Project::class);
or
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::get('/', function (Project $project) {
return view('projects.show', [
'project' => $project,
]);
});
Route::group([
'prefix' => 'settings',
], function () use ($project) { // this is the one line I am not sure of
Route::get('/', function (Setting $setting) use ($project) {
return view('project.settings.show', [
'project' => $project,
'settings' => $settings
]);
});
})->can('update', Project::class);
})->can('view', Project::class);
Way I think it should be done
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::middleware(['can:update, project'])->group(function () {
Route::get('settings', function() {
// do something
})
});
})->can('view', Project::class);
In my Laravel-5.8 application, I have a multi-company application using a single database. Each table have a company_id derived from the company table as shown below:
id | company_name | subdomain
1 | Main |
2 | Company1 | company1
3 | Company2 | company2
Main=> localhost:8888/myapp
Company1=>localhost:8888/company1.myapp
Company2=>localhost:8888/company2.myapp
I created a middleware:
class VerifyDomain
{
public function handle($request, Closure $next)
{
$domain == "myapp"; // your company app name
$path = $request->getPathInfo(); // should return /company1.myapp or /company2.myapp or /myapp
if (strpos($path, ".") !== false) { // if path has dot.
list($subdomain, $main) = explode('.', $path);
if(strcmp($domain, $main) !== 0){
abort(404); // if domain is not myapp then throw 404 page error
}
} else{
if(strcmp($domain, $path) !== 0){
abort(404); // if domain is not myapp then throw 404 page error
}
$subdomain = ""; // considering for main domain value is empty string.
}
$company = Company::where('subdomain', $subdomain)->firstOrFail(); // if not found then will throw 404
$request->session()->put('subdomain', $company); //store it in session
return $next($request);
}
}
Already, I have two (2) route groups in the route/web.php wgich looks like this:
Route::get('/', ['as' => '/', 'uses' => 'IndexController#getLogin']);
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// Config Module
Route::group(['prefix' => 'config', 'as' => 'config.', 'namespace' => 'Config', 'middleware' => ['auth']], function () {
Route::resource('countries', 'ConfigCountriesController');
Route::resource('nationalities', 'ConfigNationalitiesController');
});
// HR Module
Route::group(['prefix' => 'hr', 'as' => 'hr.', 'namespace' => 'Hr', 'middleware' => ['auth']], function () {
Route::resource('designations', 'HrDesignationsController');
Route::resource('departments', 'HrDepartmentsController');
Route::resource('employee_categories', 'HrEmployeeCategoriesController');
});
I have 2 issues:
If subdomain field is null, then the route should be for main domain: Main=> localhost:8888/myapp else localhost:8888/company1.myapp or localhost:8888/company2.myapp
2.How do I accomodate the route groups above into this:
Route::domain('localhost:8888/myapp')->group(function () {
Route::get('/', function ($id) {
//
});
});
Route::domain('localhost:8888/{subdomain}.myapp')->group(function () {
Route::get('/', function ($company_name, $id) {
$company = Company::where('subdomain', $subdomain)->firstOrFail();
// send the value of $company to data to send different view data
});
});
I'm not really sure that i understand you clearly. But, I hope you'll understand me :)
First thing is you're "domain". I suppose it's not real domain, but just uri. And maybe you should use it something like that:
Auth::routes();
$defaultDomain = config('myconfig.default_domain_name', 'myapp');
// I'm not reccomend to you use localhost:8888 here.
Route::domain('localhost:8888')
->group([
'middleware' => ['veryfy_domain'] // Add your VerifyDomain Middleware here
], function () {
// Here you already have a 'subdomain' param in session
// If you need special logic for default domain, you can out it here
Route::group(['prefix' => '/' . $defaultDomain], function () {
Route::get('/', function ($id) {
//
});
});
// Code below will work only with companies.
Route::group(['prefix' => '/{test}.' . $defaultDomain], function () {
Route::get('/', ['as' => '/', 'uses' => 'IndexController#getLogin']);
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// Config Module
Route::group(['prefix' => 'config', 'as' => 'config.', 'namespace' => 'Config', 'middleware' => ['auth']], function () {
Route::resource('countries', 'ConfigCountriesController');
Route::resource('nationalities', 'ConfigNationalitiesController');
});
// HR Module
Route::group(['prefix' => 'hr', 'as' => 'hr.', 'namespace' => 'Hr', 'middleware' => ['auth']], function () {
Route::resource('designations', 'HrDesignationsController');
Route::resource('departments', 'HrDepartmentsController');
Route::resource('employee_categories', 'HrEmployeeCategoriesController');
});
});
});
And about your middleware. I see it smth like that:
class VerifyDomain
{
public function handle($request, Closure $next)
{
$request->get('domain_name', $this->getBaseDomain());
$company = Company::where('subdomain', $subdomain)->firstOrFail();
$request->session()->put('subdomain', $company);
return $next($request);
}
// Better to store it in config
protected function getBaseDomain()
{
return config('myconfig.default_domain_name', 'myapp');
}
}
If you really want to use different domains, I think you need in your nginx something like this:
server_name *.myapp myapp;
And of course in your hosts file.
Than you can check it like that:
http://company.myapp
http://company1.myapp
http://myapp
Config example:
Create new file your_project_dir/app/config/myconfig.php (name it as you want)
Put this code in the file:
return [
'default_domain_name' => 'myapp'
];
Now you can use in in youre code as i suggest:
config('myconfig.default_domain_name');
I have question about Laravel.
I want display SEO tag automatically from Database but I do not know how to do.
I have route like this
Route::get('/', [
'uses' => 'SeoController#index',
'as' => 'homepage'
]);
Route::get('/about', [
'uses' => 'SeoController#index',
'as' => 'about'
]);
From SeoController I want to display view base on Route url;
public function index()
{
switch ($route) {
case '/':
$title = "Homepage";
return view('welcome', ['title'=> $title]);
break;
case '/about':
$title = "About page";
return view('about', ['title'=> $title]);
break;
default:
break;
}
}
How can I check $route to know which route come?
Thank you so much
I would love to suggest a better way of doing this in Laravel.
In Laravel, you would want to define different controller methods for each pages and return a view like so:
class SeoController extends Controller
{
public function home()
{
return view('home');
}
public function about()
{
return view('about');
}
public function contact()
{
return view('contact');
}
}
Ensure you have the routes registered in web.php as:
Route::get('/', [
'uses' => 'SeoController#home',
'as' => 'homepage'
]);
Route::get('/about', [
'uses' => 'SeoController#about',
'as' => 'about'
]);
Route::get('/contact', [
'uses' => 'SeoController#contact',
'as' => 'contact'
]);
And also ensure you have the corresponding blade file for each of these views in the view folder.
In api.php I've described some routes. GET method works. Can't tell the same about POST method.
<?php
use Illuminate\Http\Request;
use App\UserUnfo;
Route::middleware('auth:api')->get('/user', function (Request $request)
{
return $request->user();
});
Route::get('/person', function() {
$person = [
'ip' => '127.0.0.1',
'name' => 'me'
];
return $person;
});
Route::post('/person', function(Request $request) {
$userInfo = UserInfo::create([
'name' => $request->input('name'),
'ip' => $request->input('ip')
]);
return $userInfo;
});
In web.php
Route::get('/home', 'HomeController#index')->name('home');
The error I've got
Class 'UserInfo' not found
You're using the wrong model it's spell mistake.
use App\UserUnfo;
To
use App\UserInfo;
Here is the code.If sign in it goes to /dashboard route. but after I go to other route user session is not persisting(by dd I found this).thanks in advance if you solve, I spent hours on this.
Route::group(['middleware' => 'web'],function(){
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::get('/dashboard' , [
'uses' => 'UserController#GetDashboard',
'as' => 'dashboard'
]);
Route::post('/signin' , [
'uses' => 'UserController#postSignin',
'as' => 'signin'
]);
});
in my login controller
public function postSignin(Request $request)
{
if(Auth::attempt(['email' => $request['email'],'password' => $request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
$request is an object, not an array. Try using $request->get('email').