Controller Routing with Parameters - laravel

got a little n00b problem with Laravel 4. I have following routes:
Route::get('search', 'MovieController#search');
Route::get('edit/{$id}', 'MovieController#edit');
Route::get('/', 'MovieController#index');
and the following controller:
class MovieController extends BaseController {
protected $layout = 'layouts.master';
public function index()
{
$movies = Movie::paginate(30);
return View::make('index')->with('movies', $movies);
}
public function search()
{
if(isset($_REQUEST['sq'])) {
Cache::forever('sq', $_REQUEST['sq']);
}
$movies = Movie::where('title', 'LIKE', '%'.Cache::get('sq').'%')->paginate(30);
return View::make('index')->with('movies', $movies);
}
public function edit($id) {
return View::make('edit')->with('id', $id);
}
}
Now a call like this won't work:
<a href="edit/{{ $movie->movie_id }}">
I get a "NotFoundHttpException". The URL seems right: laravel/public/edit/2 e.g.
If i remove all the $id stuff from the code, so I route only to edit, it works.
Hopefully I could express myself enough, so somebody can help me. It's driving me nuts.
regards

In your routes.php, it's not Route::get('edit/{$id} ... but Route::get('edit/{id}

Related

Apply Middleware auth to all routes except `editPostJob/id` in Laravel

I am trying to apply auth middleware to all routes except "editPostJob" route but it didnt work as there is an id in url(http://127.0.0.1:8000/editPostJob/1).
Everytime i tried to go to that link it redirects me to login page.
in controller I tried:
public function __construct()
{
$this->middleware('auth')->except(['index', 'confirm','editPostJob']);
}
but it didnt work.
Any idea what should i do ?
thanks for any help.
I was able to solve it by doing:
public function __construct()
{
$this->middleware('auth')->except(['index', 'confirm', 'yourMethod']);
}
public function yourMethod(Request $request) //select statement
{
if (\Auth::check() == false) {
$is_read = "true";
} else {
$is_read = strip_tags($this->isLoggerOwnerOfPost($request->id));
}
$jobs = DB::table('jobs')->where('job_id', $request->id)->get();
return view('editPostJob', compact('jobs'))->with('is_read', $is_read);
}
Thanks Anurat !

One-to-many and one-to-many relationship 1 laravel

i did this(sorry my english it's bad bad....)
controller:
public function index()
{
$lessons = course::find(1)->lesson;
return view('home',compact('lessons'));
}
model lesson
public function course() {
return $this->belongsTo(Course::class);
}
model courses
public function lesson() {
return $this->hasMany(Lesson::class);
}
blade
#foreach ($lessons as $lesson )
<h4>{{$lesson->title}}</h4>
#endforeach
in browser nothing appears
why?:(
First rename your lesson method with plural lessons.
// Course model
public function lessons() // plural
{
return $this->hasMany(Lesson::class);
}
Now get the lesson's collection.
public function index()
{
$lessons = Course::find(1)->lessons;
return view('home', compact('lessons'));
}
#foreach ($lessons as $lesson )
<h4>{{$lesson->course->title}}</h4>
#endforeach
Have you tried it like this:
return view('greeting')->with('lessons', $lessons);
Now you can call '$lessons' in your view. Take a look at this link
https://laravel.com/docs/5.6/views
Can you try to use return $lessons; and show me what it says?

Passing shared variable after login with Laravel 5.5

i created a method in order to share datas with all views of my application.
For this i created a class EntityRepository where i store the datas I want to share with all views.
Those data are displayed in the layout NOT the view.
class EntityRepository
{
use App\Valuechain;
public function getEntities()
{
$vcs = Valuechain::select('valuechains.id', 'lang_valuechain.vcname', 'lang_valuechain.vcshortname')
->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
->where('langs.isMainlanguage', '=', '1')
->whereNull('valuechains.deleted_at')
->get();
return $vcs;
}
}
When I want to send datas to the methods I simply call the getEntities() method... For example :
public function index(EntityRepository $vcs)
{
$entitiesLists = $vcs->getEntities();
// My code here ...
return view('admin.pages.maps.sectors.index', compact('entitiesLists', 'myVars'));
}
In this specific case it works fine and i don't have issue. My issue concerns the landing page after login.
In the loginController :
I defined the redirectTo variable this way :
public $redirectTo = '/admin/home';
For specific reasons I had to override the authentificated() method in the LoginController in order to check if my app is configured or need to be setup ...
protected function authenticated(Request $request, $user)
{
$langCount = Lang::count();
if ($langCount == 0) {
return redirect()->to('admin/setup/lang');
}
else {
//return redirect()->to('admin/home');
return redirect()->action('BackOffice\StatsController#index');
}
}
The concerned index() method is sending the variable onto the view :
public function index(EntityRepository $vcs)
{
$entitiesLists = $vcs->getEntities();
return view('admin.home', compact('entitiesLists'));
}
Whatever the return i make i have error message...
Undefined variable: entitiesLists (View: C:\wamp64\www\network-dev\resources\views\admin\partials\header-hor-menu.blade.php)
I finally solved this issue by changing my routes :
Route::group(['prefix' => 'admin'], function () {
Route::get('/', function (){
$checkAuth = Auth::guard('admin')->user();
if ($checkAuth) {
return redirect('/admin/main');
}
else {
return redirect('admin/login');
}
});
});
In my loginController i changed :
public $redirectTo = '/admin/home';
to :
public $redirectTo = '/admin/main';
Finally :
protected function authenticated(Request $request, $user)
{
$langCount = Lang::count();
if ($langCount == 0) {
return redirect()->to('admin/setup/lang');
}
else {
return redirect()->to('admin/main');
}
}

Laravel route with variable

I have controller with show function:
class CssController extends BaseController
{
public function show($id)
{
$csstablepost = CssTable::findOrFail($id);
return View::make('posts/csspost1', compact('csstablepost'));
}
}
And my route:
Route::get('/css3/{id}', 'CssController#show' );
Everything is working fine and corect id is in route, but I want get $title in my route. I have title and id column in database. Why my route isn't working with title if i change all id to title, but works with id? Is there a way, to get my title in route name?
I'm assuming you are changing your query to CssTable::findOrFail($title); and findOrFail() looks for the PK on your table. You'll want to change this too something like CssTable::where('title', '=', $title)->firstOrFail();.
Full Example:
class CssController extends BaseController
{
public function show($title)
{
$csstablepost = CssTable::where('title', '=', $title)->firstOrFail();
return View::make('posts/csspost1', compact('csstablepost'));
}
}
Route::get('/css3/{title}', 'CssController#show');
See more documentation.
Try with following code-
class CssController extends BaseController
{
public function show($id)
{
$csstablepost = CssTable::where('id', '=', $id)->first();
return View::make('posts/csspost1')->with('csstablepost', $csstablepost);
}
}
Now go to route page-
Route::get('css3/{title}', 'CssController#show');
Hope, it will work.

CodeIgniter routing issue, advice how to do it

I'm not CI programmer, just trying to learn it. Maybe this is wrong approach, please advice.
my controller(not in sub directory) :
class Users extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index($msg = NULL) {
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function process_logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}
And a route for login :
$route['user/login'] = 'users/index';
Problem is when I wanna logout, it shows me 404 because I do not have it in my route :
$route['user/process_logout'] = 'users/process_logout';
and in my view I put logout
When I add that, it works, and that is stuppid to add a route for everything. What I'm I doing wrong, please advice.
Thank you
Don't know why you are trying to implement login feature in index() function. However since you said you are learning CI I'm telling something about _remap() function.
Before that. You can try the following routing:
$route['user/:any'] = 'users/$1';
$route['user/login'] = 'users/index';
If you want to take value immediately after controller segment you need to use _remap() function and this function may be solve your routing problem, i mean you don't need to set routing. Lets implement your code controller 'users' using _remap() function.
class Users extends CI_Controller {
private $sections = array('login', 'logout');
function __construct() {
parent::__construct();
}
public function _remap($method)
{
$section = $this->uri->segment(2);
if(in_array($section, $this->sections))
call_user_func_array(array($this, '_'.$section), array());
else show_404(); // Showing 404 error
}
private function _login()
{
$msg = $this->uri->segment(3);
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function _logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}

Resources