I would like to go to specific object view from main page(index). Here is how I communicate with page which is about to be showed
{{ route('object',['id'=>$object->id]) }}
Error message:
Error message says that it should be something wrong with controller, but in previous case this code worked.
web.php(routes)
Route::get('/object/{id}','FrontendController#object')->name('object');
My FrontendController
use App\Interfaces\FrontendRepositoryInterface;
class FrontendController extends Controller
{
public function __construct(FrontendRepositoryInterface $frontendRepository)
{
$this->fR = $frontendRepository;
}
public function index()
{
$objects = $this->fR->getObjectsForMainPage();
//dd($objects);
return view('frontend.index',['objects'=>$objects]);
}
public function object($id)
{
$object = $this->fR->getObject($id);
//dd($objects);
return view('frontend.object',['object'=>$object]);
}
FrontendRepository
use App\Models\EventObject;
use App\Models\Photo;
use App\Interfaces\FrontendRepositoryInterface;
class FrontendRepository implements FrontendRepositoryInterface {
public function getObjectsForMainPage()
{
return EventObject::with(['photos','clubs'])->get();
}
public function getObject($id)
{
return EventObject::with(['photos','clubs'])->get();
}
}
Solution was to clear routes cache using php artisan route:cache and to add missing / in route
Route::get('/object/'.'{id}','FrontendController#object')->name('object');
Related
I just got stuck creating routes with localization.
The language should be read from the URL:
domain/en/cars/
domain/fr/cars/
domain/en/cars/create
domain/fr/cars/edit
...
I use the resource method to create the routes:
Route::resource('cars','CarsController');
Similar to Route::get, how can I use placeholders in the URL to specify the language?
I don't want to start with changing all routes to "Route::get". Or is there a better way to implement localization?
Route resources declaration also allows you to specify routes placeholders the way you would in a get route declaration
Route::resource("/domain/{language}/cars", "CarsController");
and within your CarsController, use it as follows
class CarsController
{
public index ($language)
{
App::setLocale($language);
...
}
public show ($language, $id)
{ ... }
}
Or better still
class CarsController
{
public __construct ()
{
if ($currentRoute = Route::current()) {
$language = $currentRoute->getParameter('language');
App::setLocale($language);
}
// ...
}
public show ($language, $id)
{ ... }
public show ($language, $id)
{ ... }
}
Edit: Added the setting of locale. in both the constructor and in each Controller method
My project worked fine on localhost, but once it was uploaded to a Linux shared host, I got the following error.
Class 'App\Application\Model\Slider' not found
This problem occurred in three of my models: sections, projects, Slider. The other models work fine.
HomeController.php
namespace App\Application\Controllers;
use App\Application\Model\Page;
use App\Application\Model\projects;
use App\Application\Model\section;
use App\Application\Model\Slider;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth')->except(['getPageBySlug', 'welcome']);
}
public function index()
{
return view('website.home');
}
public function getPageBySlug($slug)
{
$page = Page::where('slug', $slug)->first();
if ($page) {
return view('website.page', compact('page'));
}
return redirect('404');
}
public function welcome()
{
$sections = \App\Application\Model\section::limit(3)->orderBy('id')->get();
$project = \App\Application\Model\projects::limit(3)->orderBy('id')->get();
$sliders = \App\Application\Model\Slider::get();
return view('website.welcome', compact('projects', 'sections', 'sliders'));
}
}
Namespaces are loaded through an autoload file. When you push to your shared hosting, you'll likely have to run 'composer dump-autoload' in the project root to compile this file.
Tried looking for the answer to this everywhere but having no luck so far...
Basically I want my web route to use a slug for its URL, but I want to use ID for the API route. So...
http://myurl.com/chapter/my-chapter-slug
and
http://myurl.com/api/chapter/1234
Have tried various combinations of things in the getRouteKeyName method (if(Request::route()->named('myapiroute'), if(Request::isJson() etc...) but I think these might be being checked against the page it's running on, rather than the route I'm trying to generate?
I'm thinking maybe I need to extend the base model to have a separate one to use with my API maybe?
So I'd have...
class Chapter extends Model
{
public function getRouteKeyName()
{
return 'slug';
}
....
}
and then...
class ApiChapter extends Chapter
{
public function getRouteKeyName()
{
return 'id';
}
....
}
But not sure how I'd structure this in the most "Laravel" way? Or is there a better/tidier solution?
define your route for example like
Route::get('chapters/{chapter}','ChapterController#show'); // find by slug
Route::get('api/chapters/{chapter}','ApiChapterController#show'); // find by id
for web controller
class ChapterController extends Controller
{
public function show(Request $request,$slug)
{
$instance = Model::whereSlug($slug)->first();
}
}
for api
class ApiChapterController extends Controller
{
public function show(Request $request,$id)
{
$instance = Model::find($id);
}
}
You can define 2 different routes for that but unfortunatelly you will not be able to use model binding and you will have to look for the model like:
public function show(Request $request,$slug) {
$instance = Model::whereSlug($slug)->first();
}
as shown below: https://stackoverflow.com/a/48115385/6525417
In laravel I want to call a function within function to make recursive.I caught the route error.how to call the function 'recursive in tableFetch'
class queryTest extends Controller
{
public function tableFetch() {
recursive();
}
function recursive(){
//condition
}
}
I want to do it for check the manager of the given person and then get the manager of the fetched value in query so need to do it recursive
A controller is not a good place for this. Instead, manage it in your Person Model(or whatever you have).
Everyone has a manager. So, your model has HasOne relation to itself.
Person Model:
public function manager()
{
return $this->hasOne(Person::class, 'manager_id');
}
Now if you need to check the manager of given person untill you meet a certain condition you can do it inside the model and get the result in the controller.
public function checkManager()
{
$manager = $this->manager
if (check manager)
return $manager;
//check for the last manager
return $this->manager ? $this->checkManager() : null;
}
Inside controller
function index()
{
$person = Person::find($id);
$manager = $person->checkManager();// this will do the recursive you need
}
Do something like this
class queryTest extends Controller
{
public function tableFetch() {
$this->recursive();
}
function recursive(){
//condition
}
}
you need to ask more precise details about your needs, because Laravel has some complications.
try doing this :
class queryTest extends Controller
{
public function tableFetch() {
$this->recursive();
}
public function recursive() {
//condition
}
}
I have a CustomController. For not to repeat myself, I defined getVars function in BaseController. I want to call getVars function from some functions in CustomController.
However, Laravel returns 404 error without any exception. What is wrong?
class BaseController extends Controller {
public function getVars($var1, $var2, $var3=Null, $var4=Null) {
return [];
}
}
class CustomController extends BaseController {
public function doBla1($var1, $var2) {
$vars = $this->getVars();
}
public function doBla2() {
$vars = $this->getVars();
}
public function doBla3() {
$vars = $this->getVars();
}
}
Sorry :( I found the reason of error. The names of doBla1 and getVars functions are same. This results in a 404 error. Sorry :(
$this is useful when you have method/function defined in same controller.
For functions inside parent controller you can use
Parent::getVars($var1, $var2)