Laravel 5.6 Route Group - php

My controllers which are HomeController and BlogController in Admin folder. My views like:
/admin
index.blade.php
/blog
index.blade.php
I want to call /admin0admin url to /resources/views/admin/index.blade.php.
I want to call /admin0admin/blog url to /resources/views/admin/blog/index.blade.php
Here how i call in view:
<a href="{{ route('admin0admin.blog') }}" class="br-menu-link">
And my routes like:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin0admin'], function () {
Route::get('/', 'HomeController#index')->name('index');
Route::group(['prefix' => 'blog'], function () {
Route::get('/', 'BlogController#index')->name('index');
});
});
And my BlogController index method:
return view('admin.blog.index');
I got an 404 not found error.
Route [admin0admin.blog] not defined
Laravel Version is : 5.6.*

You need to name the route admin0admin.blog, not index. prefix does not affect names of routes, so you need to write it out.

Related

Laravel - Route resource update functions has not working

I could not run the in a resource route, Controller > Update function. When I try send a form to the update, the page is return to back. Even if the update functions is completely empty. But the functions has not be triggered.
My codes:
// Controller
public function update(UserRequest $r, $id)
{
die('asdasd');
}
// HTML
<form class="ban-form" style="visibility: hidden" action="{{ route('user.update', $user->id) }}" method="POST">
#method('PUT')
#csrf
</form>
The problem is probably in your UserRequest file.
Your request doesn't not pass the validation. Therefore you are redirected back to your form.
First, try to replace UserRequest with a simple Request in order to check if that's the problem and if it is, triple check your validations and dump the content of the $errors (using {{ dump($errors) }}) variable that is injected in your view when the validation is failling.
This is the full documentation on this topic: https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors.
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
class_alias('App\Http\Controllers\HomeController', 'HomeController');
class_alias('App\Http\Controllers\AdminController', 'AdminController');
class_alias('App\Http\Controllers\FormController', 'FormController');
class_alias('App\Http\Controllers\ActionController', 'ActionController');
class_alias('App\Http\Controllers\UserController', 'UserController');
Route::get('/', 'HomeController#index')->name('home');
Route::get('giris-yap', 'HomeController#login')->name('user.login');
Route::get('kayit-ol', 'HomeController#register')->name('user.register');
Route::get('sifremi-unuttum', 'HomeController#forgotPassword')->name('user.forgot-password');
Route::get('hesabim', 'HomeController#account')->name('user.account');
Route::get('satin-al/{id}', 'HomeController#buy')->where('id','[0-9]+')->name('user.buy');
Route::get('satin-al/adim/2', 'HomeController#pay')->name('pay');
Route::get('{slug}', 'HomeController#slug')->name('slug');
Route::prefix('action')->group(function()
{
Route::get('logout', 'ActionController#logout')->name('user.logout');
});
Route::prefix('form')->group(function()
{
Route::post('login', 'FormController#login')->name('form.user.login');
Route::post('forgot-password', 'FormController#forgotPassword')->name('form.user.forgot-password');
Route::post('pay/{method}', 'FormController#pay')->where('method','(transfer|credit-card|crypto-currency)')->name('form.user.pay');
Route::post('contact', 'FormController#contact')->name('form.contact');
Route::post('update/pp', 'FormController#updatePP')->name('form.user.update.pp');
Route::post('update/profile', 'FormController#updateProfile')->name('form.user.update.profile');
});
Route::prefix('admin')->group(function()
{
Auth::routes();
Route::get('/index', 'App\Http\Controllers\AdminController#index')->name('admin.login');
Route::group(['middleware' => 'auth'], function () {
Route::resource('user', 'UserController');
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'App\Http\Controllers\ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'App\Http\Controllers\ProfileController#update']);
Route::get('upgrade', function () {return view('pages.upgrade');})->name('upgrade');
Route::put('profile/password', ['as' => 'profile.password', 'uses' => 'App\Http\Controllers\ProfileController#password']);
});
});

How to Map new route to specific old route in Laravel?

I have following route group for admin panel
Route::prefix('admin')->group(function (){
.
.
.}
I want to wrap this route to a new route e.g asda12asda
so that old behavior :
/admin/users
is changed to :
/asda12asda/users
not allowing old route. I don't want to change it internally from the system and want to find some efficient Laravel way to achieve it.
Redirect the old route to a new route
Route::prefix('admin')->group(function (){
Route::any('login', function () {
// Redirect to new route
redirect()->route('new route');
});
});
by creating a new route and mapping it accordingly
Route::prefix('asda12asda')->group(function () {
Route::any('login', function () {
// Do whatever you were about to do
})->name('new route');
});
If you are using Laravel 5.4 then you can add a new route file. Assume your route name is
adsp.php then add it to RouteServiceProvider.php like this.
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/adsp.php.php');
});
}

How can i direct link into page in laravel 5.4

AdminLTE Laravel template screenshoot:
how can i direct the link into my page in folder lapor/one.blade.php and lapor/two.blade.php?
<li class="treeview">
<a><i class='fa fa-file'></i> <span>Laporan</span> <i class="fa fa-angle-left pull-right"></i></a>
<ul class="treeview-menu">
<li>One</li>
<li>Two</li>
</ul>
</li>
Make a route like below
Route::get('one', function () {
return view('lapor.one');
});
Route::get('two', function () {
return view('lapor.two');
});
And link it like below
<li>One</li>
I would group your adminLTE routes:
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function()
{
Route::get('/', ['as' => 'dashboard', 'uses' => 'AdminController#index']);
Route::get('users', ['as' => 'user', 'uses' => 'AdminController#users']);
});
We prefixed those routes with /admin/ or whatever you want to call it. Then we prefixed their name with admin (using 'as').
Now get a specific route url:
{{ route('admin.dashboard') }}
Why do it like this?
Naming your routes is very important because if the route url changes and your app has hardcored urls (like url('/admin/dashboard') your entire application will break. With named routes this wont happen.
You can do it in three step:
make a function in your controller.like below
publice function functionName(){
return view('yourpagename(one)');
}
go to routes folder open web.php and connect with your controller function in routes. like
Route::get('page-name', 'controllerName#functionName');
add this url to your view page link tag
{{URL::to('page-name')}}
Hope it will works fine.
Before going to redirect the page two steps you need to do :
Step 1:
Define Methods in controller(named as SampleController) for example:
//Controller Name:SampleController
// Method Names defined in controller :lapor1,lapor2
//Method 1
public function lapor1(){
return view('lapor.one');
}
//Method 2
public function lapor2(){
return view('lapor.two');
}
Step :2
Define Routes for the pages like below:
Route::get('lapor1', ['as' => 'laporone','uses'=>'SampleController#lapor1']);
Route::get('lapor2', ['as' => 'laportwo','uses'=>'SampleController#lapor2']);
Step 3:
Link up to view pages now:
<li>One</li>
<li>Two</li>

Skipping a middleware for a particular route inside a route group in Laravel

I Want to skip the middleware for a particular route in a route group. How can I do this?
Route::group(['prefix' => 'testgroup','middleware' => ['login.oauth']],function (){
Route :: get('/', 'testController#index');
Route :: get('/api','testController#apiCall');
});
I want to skip the 'login.oauth' middleware for the Route :: get('/api','testController#apiCall')
Please keep that testgroup function must be accessible to all routes and middleware function to particular(some other route) in the same function
Route::group(['prefix' => 'testgroup'], function () {
Route::group(['middleware' => ['login.oauth'], function() {
Route :: get('/', 'testController#index');
});
Route :: get('/api','testController#apiCall');
});
Just create a second group without the middleware:
Route::group(['prefix' => 'testgroup','middleware' => ['login.oauth']],function (){
Route :: get('/', 'testController#index');
});
Route::group(['prefix' => 'testgroup'],function (){
Route :: get('/api','testController#apiCall');
});

Laravel error exception route not defined

I start a new laravel project make composed by two pages, I created the pages under the app/view directory and this is my route.php file:
Route::get('/', function()
{
return View::make('hello');
});
Route::get('welcome', function()
{
return View::make('welcome');
});
Route::any('signup', function()
{
return View::make('signup');
});
I can access to the pages signup by taping the link directly in the browser and also when I run artisan routes it shows me the routes that I created.
in the welcome.blade.php when I add the line
{{link_to_route('signup')}}
and reload the page I have this error
ErrorException
Route [signup] not defined. (View: C:\wamp\www\atot\app\views\welcome.blade.php)
how can I solve this problem?
Try this instead:
Route::any('signup', [
'as' => 'signup',
function() {
return View::make('signup');
}
]);
Your problem was that you didn't use a named route.
If you want, you can read more about it here: http://laravel.com/docs/routing#named-routes
Link_to_route is a method that generates a url to a given named route, so to make it work you can name each of your routes and then it will work
link_to_route('route.name', $title, $parameters = array(), $attributes = array());
In routes.php update the following
Route::get('/', array('as'=>'home', function()
{
return View::make('hello');
}));
Route::get('welcome', array('as'=>'welcome', function()
{
return View::make('welcome');
}));
Route::any('signup', array('as'=>'signup', function()
{
return View::make('signup');
}));
Then you can generate the following routes:
{{link_to_route('home')}}
{{link_to_route('welcome')}}
{{link_to_route('signup')}}
You should use either:
{{ link_to('signup') }}
Or declare the route using a name
Route::any('signup', array('as' => 'signup', function()
{
// ...
}));
The link_to_route helper function only works with a named route which accepts a route name in the first argument.

Categories