I want to go from home.blade.php to all_article.blade.php, but they tell me that such a route was not found. What am I doing wrong. Thanks in advance for your help.
blog/home.blade
<i class="fa fa-plus-square-o"></i> Add
BlogController
public function articlesAll_blade(){
return view('blog.all_article',[
'articles' => Article::orderBy('created_at', 'desc')->paginate(10),
'footers' => System::all(),
]);
}
web.php
Route::get('/', 'BlogController#articlesAll', function () {
return view('blog.home');
});
Route::get('/all_article', 'BlogController#articlesAll_blade', function () {
return view('blog.all_article');
});
what you are missing is a route name, add a name to your route
Route::get('/all_article', 'BlogController#articlesAll_blade', function () {
return view('blog.all_article');
})->name('blog.all_article');// see the name part
doc link https://laravel.com/docs/routing#named-routes
Route::get('/all_article','BlogController#articlesAll_blade')->name('blog.all_article');
Related
I’m currently working on a new website and want to use multi language on different pages and the URL should include the prefix.
This is my goal and should be achieved:
Mywebsite/ --> Mywebsite/en
Mywebsite/en --> Should show the main page in en
Mywebsite/de --> Should show the main pahe in de
Mywebsite/en/legal --> Should show legal page in en
Mywebsite/legal --> Should show the legal page in the before selected language and redirect to Mywebsite/en/legal
So if the prefix is missing the site should be redirected and show the exact page in the correct language.
I tried to implement this way but not all cases are working.
web.php
Route::get('/', function () {
return redirect(app()->getLocale());
});
Route::get('/legal', function () {
return redirect(app()->getLocale() . '/legal');
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'localization'], function($locale) {
Route::get('/', function () {
return view('home');
});
Route::get('/home', function () {
return view('home');
});
Route::get('/legal', function () {
return view('legal');
});
});
Localization.php
public function handle($request, Closure $next)
{
app()->setLocale($request->segment(1));
return $next($request);
}
app()->getLocale() does not work at all
Route::get('/legal', function () is also not working
What is missing or what can I change to solve it?
Thanks in advance!
the controller is returning a blank data/view and I think something is wrong with my routes. if I remove {locale}, the data is retrieved.
Can anyone help with returning the data properly while my routes have {locale} in it? Here are my related code:
Web.php
Route::get('{locale}/projects/{id}/billings', 'ProjectController#showbilling')
->name('showbilling');
Route::post('{locale}/projects/{id}', 'ProjectController#addbilling')
->name('addbilling');
ProjectController.php
public function showbilling($id)
{
$billings = Project::find($id);
$locale = app()->getLocale();
return $billings;
//return view('admin.addbillings', compact('billings'));
}
Edit: Here's my full web.php
web.php
Route::get('/', function() {
return redirect(app()->getLocale());
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function () {
Route::get('/', function () {
return view('welcome');
})->name('main');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
//Customers
Route::get('/customers', 'CustomerController#showcust')->name('customers');
Route::post('/sendcust', 'CustomerController#sendcust')->name('sendcust');
//Items
Route::get('/items', 'ItemController#showitems')->name('items');
Route::post('/senditem', 'ItemController#senditem')->name('senditem');
//Projects
Route::get('/projects', 'ProjectController#showprojects')->name('projects');
Route::post('/sendproj', 'ProjectController#sendproj')->name('sendproj');
//ProjectBillings
Route::get('/projects/{id}/billings', 'ProjectController#showbilling')->name('showbilling');
Route::post('/projects/{id}', 'ProjectController#addbilling')->name('addbilling');
//Invoices
Route::get('/invoices', 'InvoiceController#showinvoice')->name('invoices');
Route::post('/sendinvoitem', 'InvoiceController#sendinvoitem')->name('sendinvoitem');
Route::get('/invoices/{id}/details', 'InvoiceController#showdetails');
Route::post('/updateitem','InvoiceController#updatedetail')->name('updateitem');
Route::get('invoices/{id}/generate', 'InvoiceController#generate');
Route::post('/updatestatus', 'InvoiceController#changestatus')->name('updatestatus');
});
You are passing 2 params in your route but accepting only 1 in the controller. Add locale.
public function showbilling($locale, $id)
I am getting this error (Route [companies.show] not defined.) and I don't know what to do.
Actually I am updating the data in CompaniesController and data is updating but the route is not working
Here is the code for that:
public function update(Request $request, Company $company){
$companyUpdate = Company::where('id', $company->id)->update(['name'=> $request->input('name'),'description'=> $request->input('description')]);
if($companyUpdate){
return redirect()->route('companies.show', ['company'=> $company->id])
->with('success' , 'Company updated successfully');
}
return back()->withInput();
And My web.php file is as follow `
Route::get('/', function () {
return view('welcome');});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('/company','CompaniesController');
Thanks in advance for helping me
change companies.show to
return redirect()->route('company.show', ['company'=> $company->id])
->with('success' , 'Company updated successfully');
}
companies.show is undefined because you didn't give your route a name.
Route::get('/companies/{id}', 'CompaniesController#showCompanyForID')->name('companies.show');
Create a function called showCompanyForID in your CompaniesController and return the company which has the id requested for in your Request.
use Illuminate\Http\Request;
public function showCompanyForID(Request $request)
{
$id = isset($request->id) ? $request->id : 0;
if ($id) {
// do work here
}
return view('companies.company')->with(compact('var1', 'var2'));
}
You can now redirect to that route:
return redirect()
->route('companies.show')
->with(['company'=> $company->id, 'success' => 'Company updated successfully']);
To see all routes, cd to your project in cmd / Terminal and type: php artisan route:list
I have admin prefix where url/admin/dashboard is my dashboard view.
What I need is to redirect users to url above if they type only url/admin .
This is what I have:
Route::prefix('admin')->group(function () {
Route::get('dashboard', 'HomeController#index')->name('dashboard'); //works
Route::get('/', function () {
return redirect()->route('dashboard');
}); //doesn't work
});
You might want to use this:
Route::get('url/admin/dashboard', 'HomeController#index')->name('dashboard');
Route::get('url/admin', function () {
return redirect('url/admin/dashboard');
});
You can do
Route::get('url/admin/{name?}', 'HomeController#index')
->where('name', 'dashboard')
->name('dashboard');
Or if you want to use the prefix
Route::prefix('admin')->group(function () {
Route::get('/{name?}', 'HomeController#index')
->where('name', 'dashboard')
->name('dashboard');
});
The latest Laravel made it even easier. Define the route for dashboard followed by redirect. Have a look.
Route::get('url/admin/dashboard', 'HomeController#index')->name('dashboard');
Route::redirect('url/admin', 'url/admin/dashboard');
I've a simple route into the file web.php:
Route::get('first/{param?}', [
'uses' => 'App\Http\Controllers\MyController#index',
'as' => 'myControllerIndex'
]);
Now, I'd like to create a second route that uses the first route but passing specific params.
I tried something like this:
Route::get('second', function () {
return file_get_contents(route('myControllerIndex', ['param' => 'book1']));
});
but it doesn't work.
Can anyone help me?
Thank you.
You could use a redirect
Route::get('second', function () {
return redirect()->route('myControllerIndex', ['param' => 'book1']);
});
Or you can access the controller directly
Route::get('second', function () {
return app('App\Http\Controllers\MyController')->index('book1');
});