Laravel: how to use /admin - php

I want to create pages for admin and I want to use dashboard in localhost:8000/admin url...but the page is showing a display Not Found Error.
Please tell me what should I do to resolve this

You need somethig like this in your /app/Http/routes.php
Route::group(['prefix' =>'admin', 'namespace' => 'admin', 'middleware' => 'auth'], function() {
Route::get('/', 'AdminController#index');
// .....
});

Related

fetching route from vendor through routes/web.php

I have this route included in my web.php in my routes folder.
Route::group(['prefix' => 'admin','namespace'=>'Admin', 'middleware' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'filemanager'], function () {
\UniSharp\LaravelFilemanager\Lfm::routes();
});
});
My issue is whenever I run php artisan route:list I get the following error:
Class App\Http\Controllers\Admin\UniSharp\LaravelFilemanager\Controllers\LfmController does not exist
The Lfm controller is inside my vendor folder and I've been scouting around the internet for a solution and applied many different ways for the route change and the only way I got it to work is by using the route provided by default from the package. But if I use that, I use lose my admin authentication I'm hoping someone could give me some insight as to what I'm doing wrong here?
I would really appreciate that! Thank you in advance.
I came up with this solution. I am using Laravel 5.4
Instead of putting the code in routes/web.php, put it in RouteServiceProvider, so the default namespace is not set yet;
protected function mapWebRoutes()
{
Route::group(['prefix' => 'laravel-filemanager', 'middleware' => ['web', 'auth']], function () {
\UniSharp\LaravelFilemanager\Lfm::routes();
});
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
}
For laravel 5.6 and above,
Go to config/lfm.php and set 'use_package_routes' to false.
In routes/web.php use the following routes (this is an example, might change adhering to your prefix etc.):
Route::group(['prefix' => 'admin','namespace'=>'Admin', 'middleware' => 'admin', 'as' => 'admin.'], function () {
Route::get('/filemanager', '\UniSharp\LaravelFilemanager\Controllers\LfmController#show')->name('show');
Route::any('/filemanager/upload', '\UniSharp\LaravelFilemanager\Controllers\UploadController#upload')->name('unisharp.lfm.upload');
Route::get('/filemanager/errors', '\UniSharp\LaravelFilemanager\Controllers\LfmController#getErrors')->name('getErrors');
Route::get('/filemanager/jsonitems', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#getItems')->name('getItems');
Route::get('/filemanager/move', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#move')->name('move');
Route::get('/filemanager/domove', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#move')->name('domove');
Route::get('/filemanager/newfolder', '\UniSharp\LaravelFilemanager\Controllers\FolderController#getAddfolder')->name('getAddfolder');
Route::get('/filemanager/folders', '\UniSharp\LaravelFilemanager\Controllers\FolderController#getFolders')->name('getFolders');
Route::get('/filemanager/crop', '\UniSharp\LaravelFilemanager\Controllers\CropController#getCrop')->name('getCrop');
Route::get('/filemanager/cropimage', '\UniSharp\LaravelFilemanager\Controllers\CropController#getCropimage')->name('getCropimage');
Route::get('/filemanager/cropnewimage', '\UniSharp\LaravelFilemanager\Controllers\CropController#getNewCropimage')->name('getCropimage');
Route::get('/filemanager/rename', '\UniSharp\LaravelFilemanager\Controllers\RenameController#getRename')->name('getRename');
Route::get('/filemanager/resize', '\UniSharp\LaravelFilemanager\Controllers\ResizeController#getResize')->name('getResize');
Route::get('/filemanager/doresize', '\UniSharp\LaravelFilemanager\Controllers\ResizeController#performResize')->name('performResize');
Route::get('/filemanager/download', '\UniSharp\LaravelFilemanager\Controllers\DownloadController#getDownload')->name('getDownload');
Route::get('/filemanager/delete', '\UniSharp\LaravelFilemanager\Controllers\DeleteController#getDelete')->name('getDelete');
Route::get('/filemanager/demo', '\UniSharp\LaravelFilemanager\Controllers\DemoController#index')->name('getDelete');
});

Laravel routing without appending any prefix

I have a path in Laravel it is like subdomain.mydomain.com/admin/login
I am trying to call
subdomain.mydomain.com and need to get the login page straight.
Currently, it's not working
This is the function I am using in routerserviceprovider.php
protected function mapAdminRoutes()
{
Route::middleware('subdomain.mydomain.com')
->prefix('admin')
->namespace($this->namespace)
->group(base_path('routes/admin.php'));
}
and in admin.php there is a resource group shows like this:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
//Login Routes...
Route::view('login','admin.login');
});
can anyone help with this?
Add following route
Route::get('/',function(){ return view('login.index'); })->name('admin.login');
i hope it helps

Laravel route Resource breaks another route

When I use my route like this, the test route is working fine.
Route:-
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::get('user/test','UserController#test'); // is working
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'USerController#planed']);
.
.
.
.
But when I want to use it like following, it shows a blank page :
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'UserController#planed']);
Route::get('user/test','UserController#test'); // is not working
.
.
.
.
What is my mistake?
Because the call to test is intercepted by
Route::resource('user', 'UserController');
because if you check the routes in the console with
$> php artisan route:list
you'll see it includes
GET|HEAD | user/{user}
and then your first route
Route::get('user/test','UserController#test'); // is not working
is never reached. Try to put it BEFORE the other line.
I thing the issue in naming the route check This
Try like this
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed','UserController#planed'])->name('user.planed');
Route::get('user/test','UserController#test'); // is not working
Just replace
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'UserController#planed']);
With
Route::get('user/planed','UserController#planed'])->name('user.planed');

Error Messages - Laravel 5.2

Can someone please tell me how to make my "Page not found" or something messages? For example if someone writes a link in the browser which do not exist in my project, not to show the standard error page ( Woops, something went wrong, View [bla.bla] not found ) but page specified by me?
<?php
Route::get('sendemail', 'EmailController#sendEmail');
Route::get('test', 'AuthController#getTest');
Route::get('napravisiadmin', 'ClassbookController#getIndex');
Route::group(['middleware' => ['web']], function () {
Route::group(['middleware' => ['guest']
], function () {
Route::get('login', 'AuthController#getLogin');
Route::post('login', 'AuthController#postLogin');
});
Route::get('logout', 'AuthController#getLogout');
//Admin
Route::group(['middleware' => ['auth', 'auth.admin']
], function () {
Route::group([
'prefix' => 'admin',
'namespace' => 'Admin'
], function () {
Route::controller('student', 'StudentsController');
Route::controller('profile', 'ProfilesController');
Route::controller('class', 'ClassesController');
Route::controller('subjects', 'SubjectsController');
Route::controller('teacher', 'TeachersController');
Route::controller('marktype', 'MarkTypeController');
Route::controller('rules', 'RuleController');
Route::get('{slug?}', 'PageController#getView');
});
});
//Admin
//Student
Route::group([
'middleware' => ['auth', 'auth.student'],
'prefix' => 'stu',
'namespace' => 'Stu'
], function () {
Route::get('{slug?}', 'StuController#getView');
});
//Student
//Teacher
Route::group([
'middleware' => ['auth', 'auth.teacher'],
'prefix' => 'educator',
'namespace' => 'Educator'
], function () {
Route::get('edit/{id}', 'AccountController#getEdit');
Route::post('edit/{id}', 'AccountController#saveEdit');
Route::get('account', 'AccountController#getView');
Route::get('class-subject', 'AccountController#getClassSubject');
Route::get('add-mark', 'AccountController#getAddMark');
Route::post('mark', 'AccountController#postAddMark');
Route::get('added', 'AccountController#marksList');
Route::get('statistics', 'AccountController#marksInTable');
Route::get('personalemails', 'PersonalEmailController#getView');
Route::post('personalemails', 'PersonalEmailController#personalEmail');
});
//Teacher
});
Route::get('{slug?}', 'PageController#getView');
For the "Page not found" 404 error create a view in resources/views/errors/404.blade.php and it will show when you get a 404 error.
From the documentation:
Custom HTTP Error Pages
Laravel makes it easy to return custom error pages for various HTTP
status codes. For example, if you wish to customize the error page for
404 HTTP status codes, create a resources/views/errors/404.blade.php.
This file will be served on all 404 errors generated by your
application.
The views within this directory should be named to match the HTTP
status code they correspond to.
https://laravel.com/docs/5.2/errors#custom-http-error-pages
You can always go a step further by utilising the exception handler and handling exceptions the way you desire by customising the render() method
https://laravel.com/docs/5.2/errors#the-exception-handler
For example, if you wanted to handle file not found error, Exceptions\Handler.php
public function render($request, Exception $e)
{
if ($e instanceof \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException) {
return response()->view('errors/exceptions/file-not-found', [], 500);
}
return parent::render($request, $e);
}
You can create custom error 404 page. If someone will enter wrong URL in a browser, he will see that page.
Also, you can redirect user manually to this page with:
abort(404);
Update
I guess the problem is here:
Route::get('{slug?}', 'PageController#getView');
You're using this three times, try to remove all of them.
The thing is when Laravel doesn't find any routes, it takes {slug} and passes it to the PageController, so when you enter http://example.com/sometext, you will be transferred to the PageController with slug = sometext.
If you do not want to remove it, check for slug inside a controller and if slug means something - good. If not, just abort(404); and user will be transferred to an error page.
Also, if you're on 5.2.27 of higher, remove web middleware from routes.php (it applies automatically, and manual apply can cause errors and strage behavior).

Laravel redirect to route

I'm using Laravel 5.1 and wanted to know if there is a better way to do routing redirection.
Route::get('user/login', 'UserController#login');
Route::get('login', function() {
return redirect()->to('user/login');
});
So basically, whenever a user goes to http://example.com/login, they will be redirect to http://example.com/user/login.
Is there a better way or other ways to do this or am I doing it correctly already? Thanks!
That's about as simple as it gets
You could also do redirect('user/login') to save a few characters
If you had multiple redirects like this you could handle them all at once
Route::pattern('user_path', '(login|logout)');
Route::get('{user_path}', function($user_path) {
return redirect('user/' . $user_path);
});
Route::get('new/create_view','CreateController#create_view');
Route::post('new/create_view','CreateController#insert_view');
Route::get('new/create_table','CreateController#create_table');
Route::post('new/create_table','CreateController#insert_table');
Route::get('new/create_package','CreateController#create_package');
Route::post('new/create_package','CreateController#insert_package');
This is the way i am using the route. Simple method. when the time of GET, am calling a controller function and inside that particular controller function, i have written the logical codes. In the POST also, doing the same thing.
Another way is there,GROUP Routing
Route::group(['namespace' => 'Admin'], function()
{
// Controllers Within The "App\Http\Controllers\Admin" Namespace
Route::group(['namespace' => 'User'], function()
{
// Controllers Within The "App\Http\Controllers\Admin\User" Namespace
});
});
eg:
Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
Route::resource('pages', 'PagesController', array('only' => array('index', 'store', 'show', 'update', 'destroy')));
Route::resource('users', 'UsersController');
});

Categories