Route to controller to subfolder in Laravel 5.1 - laravel

I am a beginner in Laravel and I have a specific question and I hope you can send in the right direction.
I have read and tryed a lot of solutions written in this forum with no success.
Oke lets start.
I want to change my www.domain.com/support to subdirectory www.domain.com/support/support
Created app-->Http-->Support-->SupportController.php and added:
namespace app\Http\Controllers\Support;
use app\Http\Controllers;
/**
* Display the Help Center.
*
* #return Response
*/
public function support()
{
return view('static.support');
}
Added in the app-->Http-->routes.php
Route::group(['domain' => env('APP_DOMAIN', 'domain.com'),
], function () {
require 'Routes/general.php'; });
Added in the app-->Http-->routes-->general.php
Route::get('/support', ['as' => 'static.support', 'uses' =>'Support\SupportController#support']);
Added the support.blade.php in the following folder Resources-->views-->static-->support
Thank you

Controller namespace has nothing to do with URLs. If you want to use http://www.domain.com/support/support, just use this route:
Route::get('support/support', 'SupportController#support');

namespace App\Http\Controllers\Support;
use App\Http\Controllers;
class SupportController extends Controller
{
public function support()
{
return view('static.support');
}
}
Please confirm your controller looks like my example above rather than the controller example in your question.
Also, please provide errors from your storage/logs/laravel.log

Related

Voyager and Jetstream: Login to Admin Panel leads to Dashboard Page

I just started my first Laravel project and try to combine Jetstream Authentification with Voyager Admin Panel.
First of all, I installed Jetstream on a fresh Laravel installation and it worked so far:
Afterwards, I tried to add Voyager to generate the CRUDs for my website and added a new user with
php artisan voyager:admin your#email.com --create
But whenever I tried to login through the url "../admin", I was redirected to "../dashboard" from Jetstream.
Even if I reentered "../admin" as URL, I was redirected. As long as I was logged in, I cannot enter the Voyager Backend.
So I guess it's some kind of routing / middleware issue, but I cannot find out which issue it is.
Within the web.php Routing file, there's only the basic stuff:
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
});
Not sure if that's relevant, but my IDE recognizes Voyager:: as unknown class, even it works the same way on a different Laravel installation.
But from the look of it, I expected the Route::middleware() to redirect a logged in person which types the url "../dashboard" to the Dashboard view, but nothing more. Removing this Route also didnt help the problem, so I guess I was wrong.
But beside this, only the pure Voyager Routes are left, so I'm not sure where else I can look to solve this problem.
You can add custom responses on app/Http/Responses directory.
just make new responses called LoginResponse
then use this code
<?php
namespace App\Http\Responses;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
class LoginResponse implements LoginResponseContract
{
/**
* #param $request
* #return mixed
*/
public function toResponse($request)
{
$home = auth()->user()->is_admin ? '/admin' : '/dashboard';
return redirect()->intended($home);
}
}
Then, bind your LoginResponse in FortifyServiceProvider
You can use this code
<?php
namespace App\Providers;
// ...
use App\Http\Responses\LoginResponse;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
// ...
$this->app->singleton(LoginResponseContract::class, LoginResponse::class);
}
}
i know that it to late but for other users who had the same problem.
first i installed jetstream
int .env file APP_URL=http://localhost:8000
i installed voyager with dummy data
i added manualy in table user_roles this ligne ( the admin )
INSERT INTO `user_roles` (`user_id`, `role_id`) VALUES ('1', '1');
and it work
you can see this video i found in youtube i think it will help you .
https://www.youtube.com/watch?v=UDYZx5uIwmQ

Laravel Page Route Not Using The Correct Parameters Passed In

Hey all you smart people,
Im having a issue I normally work with API routes not really used Web Routes before and finding this rather complicated for some reason :D
Ive made this route
Route::get('/test/{page?}', \App\Http\Livewire\Test::class);
sand this is my logic in the render() in the controller
public function render(Request $request, $page = 1)
{
dd($page);
}
however when I'm on the browser and type
http://url.com/test/2
The Die Dump keeps giving me page 1 all the time am i missing something here ??
Thanks for the help if anyone can help...
Update
Im not sure if its because I'm using a livewire component and not an actual controller....
Livewire Component
<?php
namespace App\Http\Livewire;
use Illuminate\Http\Request;
use Livewire\Component;
class Test extends Component
{
public function render(Request $request, $page = 1)
{
dd($page);
return view('livewire.test');
}
}
Route Parameters in livewire works like this
web.php
Route::get('/test/{page?}', \App\Http\Livewire\Test::class);
component
public function mount($page = 1)
{
dd($page);
}
ref link https://laravel-livewire.com/docs/2.x/rendering-components#route-params

Laravel get getCurrentLocale() in AppServiceProvider

I'm trying to get the LaravelLocalization::getCurrentLocale() in the boot() method of the Laravel AppServiceProvider class, and although my default locale is pt I always get the en. The package I'm using is mcamara/laravel-localization. Code I have:
public function boot()
{
Schema::defaultStringLength(191);
// Twitter view share
$twitter = Twitter::getUserTimeline(['screen_name' => env('TWITTER_USER'), 'count' => 3, 'format' => 'object']);
view()->share('twitter', $twitter);
// Current language code view share
$language = LaravelLocalization::getCurrentLocale();
view()->share('lang', $language);
// Practice Areas
view()->share('practice_areas', \App\Models\PracticeArea::with('children')->orderBy('area_name')->where(['parent_id' => 0, 'language' => $language])->get());
}
I'm probably placing this in the wrong place because when I try to share the practice_areas variable it always sets it as en even if the language is switched.
What may I be doing wrong?
Thanks in advance for any help
Faced the exact same problem, solved by using a dedicated Service Provider and a view composer class, like so:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class LocalizationServiceProvider extends ServiceProvider
{
public function boot() {
View::composer(
'*', 'App\Http\ViewComposers\LocalizationComposer'
);
}
}
and then on LocalizationComposer class:
<?php
namespace App\Http\ViewComposers;
use Illuminate\View\View;
use LaravelLocalization;
class LocalizationComposer {
public function compose(View $view)
{
$view->with('currentLocale', LaravelLocalization::getCurrentLocale());
$view->with('altLocale', config('app.fallback_locale'));
}
}
currentLocale and altLocale will be available on all views of your application
From the package docs Usage section:
Laravel Localization uses the URL given for the request. In order to achieve this purpose, a route group should be added into the routes.php file. It will filter all pages that must be localized.
You need to be setting the localization within your route group definitions:
Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
Route::get('/', function()
{
return View::make('hello');
});
Route::get('test',function(){
return View::make('test');
});
});
After several hours trying to work around the issue, I decided not to use the view()->share() with mcamara/laravel-localization package methods here. The reasons seems to be that in the AppServiceProvider::class boot() method the package isn't yet getting the requested language string.
Anyway, thank you all for your help!

Laravel can't find controller, but will report any syntax errors in said controller if they exist

just trying to get to grips with the basics of Laravel. I was getting syntax errors in my areasController file. Once they were resolved I started recieving this error: ReflectionException in Route.php line 280:
Class App\Http\Controllers\areasController does not exist. So it seems like Laravel can find the file to know that when there are errors in it, but not the rest of the time. Any help appreciated, this is my first framework so I'm pretty stumped.
routes.php:
Route::get('/', function () {
return view('welcome');
});
Route::get('locations', function() {
return view('locations');
});
Route::get('areas', ' areasController#areas');
areasController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
class areas extends Controller
{
//
public function areas() {
$areas = DB::table('areas')->all();
return view('areas');
}
}
Any help would be appriciated.
In your routes.php file, you ask to use the method areas from areasController but in your controller file, you define class areas extends Controller
It should be class areasController extends Controller then it should work

Routing in Laravel is not working

I am using laravel 5.0
I am trying to route the following. But it is not working
Route::post('accesscontrols/permissions', 'AccescontrolsController#permission');
I don't know what error in this.
It does not access permissions function in AccesscontrolsController
I have a function in AccesscontrolsController
public function permission()
{
$roles = DB::table('roles')->get();
$permissions = DB::table('permissions')->get();
return view('accesscontrols.permission', compact('roles', 'permissions'));
}
What I have did wrong?
Your route declaration should be made in app/Http/routes.php.
Also, make sure that your controller is within the App\Http\Controllers namespace and that it extends App\Http\Controllers\Controller.
Ex:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function permission()
{
...
Also, if you whish to test it in the browser (by typing "accesscontrols/permissions" in the address bar), you route should answer to the GET verb. Try to declare it using Route::get( instead.
You are returning a view in your method and you are not working with any POST data, which is strange. Are you sure you want POST request and not GET?

Resources